在WP7上传照片时,未经授权的访问异常到Picasa

问题描述:

我有以下的代码,试图上传的图片到Picasa网站。当我米试图上传我为获得未经授权的访问异常。我不知道如何得到的authToken。
这里是我的代码。请让我知道如果您有任何线索

I have the following code which tries to upload the picture to picasa website. when I m trying to upload I m getting Unauthorised access exception. I dont know how to get the AuthToken. Here is my code . Please let me know if you have any clues.

    public delegate void UploadPhotoCallback(bool success, string message);


    public static void UploadPhoto(string albumId, string originalFileName, byte[] photo, UploadPhotoCallback callback)
    {
        string Username = "mailmugu";
        string AuthToken = "";

        try
        {
            var url = string.Format("http://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}", Username, albumId);
            var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            //request.ContentType = HttpFormPost.ImageJpeg;
            //request.Method = HttpMethods.Post;

            request.ContentType = "image/jpeg";
            request.Method = "POST";
            request.Headers[HttpRequestHeader.Authorization] = "GoogleLogin auth=" + AuthToken;

            request.BeginGetRequestStream(new AsyncCallback(UploadGetRequestCallback),
                                          new UploadRequestState
                                              {
                                                  Request = request,
                                                  Callback = callback,
                                                  Photo = photo,
                                                  FileName = originalFileName
                                              });
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            //throw new MyException(MyResources.ErrorUploadingPhotoMessage, e);
        }
    }

private static void UploadGetRequestCallback(IAsyncResult ar)
    {
        try
        {
            var state = (UploadRequestState)ar.AsyncState;
            HttpWebRequest request = state.Request;

            // End the operation
            var stream = request.EndGetRequestStream(ar);

            stream.Write(state.Photo, 0, state.Photo.Length);
            stream.Close();

            request.BeginGetResponse(UploadGetResponseCallback, state);
        }
        catch (Exception e)
        {
            //throw new MyException(MyResources.ErrorUploadingPhotoMessage, e);
        }
    }

    private static void UploadGetResponseCallback(IAsyncResult ar)
    {
        UploadRequestState state = null;
        try
        {
            state = (UploadRequestState)ar.AsyncState;
            HttpWebRequest request = state.Request;

            // End the operation
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);

            if (response != null)
            {
                response.Close();
            }

            if (state.Callback != null)
            {
                MessageBox.Show("Uploaded Sucessfully");
                //state.Callback(true, MyResources.PhotosUploadedMessage);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("Error" + e.Message);
            Console.Write(e);
            //if (state != null && state.Callback != null)
            //state.Callback(false, MyResources.ErrorSavingImageMessage);
        }


    }

    public class UploadRequestState
    {
        public HttpWebRequest Request { get; set; }

        public UploadPhotoCallback Callback { get; set; }

        public byte[] Photo { get; set; }

        public string FileName { get; set; }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        string albumId = "Picasa";
        string Filename = "Test";
        UploadRequestState _uploadReq = new UploadRequestState();
        Uri myuri = new Uri("/Images/Test.jpg", UriKind.RelativeOrAbsolute);
        BitmapImage btmMap = new BitmapImage(myuri);
        image1.Source=btmMap;
        WriteableBitmap bmp = new WriteableBitmap(btmMap.PixelWidth,btmMap.PixelHeight);
        MemoryStream ms = new MemoryStream();
        // write an image into the stream
        Extensions.SaveJpeg(bmp, ms,
            btmMap.PixelWidth, btmMap.PixelHeight, 0, 100);
        byte[] Photo = ms.ToArray();
        UploadPhoto(albumId,Filename,Photo,_uploadReq.Callback);
    }
}



}

}

由于你的代码甚至不试图去你想要做什么,我建议在看的 http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html#Auth 并打开一个新的问题,解决你可能有任何顾虑。

Since your code does not even attempt to get the Authentication Token required to do what you want I suggest looking at http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html#Auth and open a new question to address any concerns you might have.