开发者

Unauthorised access exception when Uploading photo to picasa in WP7

开发者 https://www.devze.com 2023-04-10 12:10 出处:网络
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.

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);
    }
}

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消