在WP7中将照片上传到picasa时未经授权的访问异常

时间:2011-10-03 12:28:24

标签: c# windows-phone-7 uploading

我有以下代码尝试将图片上传到picasa网站。当我试图上传时,我获得了未经授权的访问异常。我不知道如何获得AuthToken。 这是我的代码。如果您有任何线索,请告诉我。

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

}

1 个答案:

答案 0 :(得分:0)

由于您的代码甚至没有尝试获取所需的身份验证令牌,我建议您查看http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html#Auth并打开一个新问题以解决您可能遇到的任何问题。