插入图片错误:远程服务器返回错误:(403)禁止 [英] Insert photo ERROR: The remote server returned an error: (403) Forbidden

查看:211
本文介绍了插入图片错误:远程服务器返回错误:(403)禁止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助!

我试着去插入一个新的照片到Picasa相册使用OAuth 2.0和一个简单的Htt prequest过程。其结果是,我不能跟随上列出的说明后插入一个新的照片到我的Picasa网络相册:的https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol#Auth

Im trying to insert a new photo into a Picasa Album using Oauth 2.0 and a simple HttpRequest process. The result is that I cant insert a new photo into my Picasa web album after following the instructions listed on: https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol#Auth

我也不得不说,我试着使用.net库,它们提供了相同的结果。

I also have to say that I tried using the .Net library that they provide with the same results.

这是我使用的是现在的执行如下:

The implementation that I'm using now is the following:

public static string PostImage(
       string streamImageConvertedToString)
    {
        string url = string.Format("https://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}", "username@gmail.com", "idAlbum");

        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.ContentType = "image/jpeg";
        request.ContentLength = Encoding.UTF8.GetByteCount(data);
        request.Method = "POST";
        request.Headers.Add("GData-Version", "2");
        request.Headers.Add("Slug", "cute_baby_kitten.jpg");
        request.Headers.Add("Authorization", "Bearer " + GetToken());
        if (data != null)
        {
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(data);
            }
        }

        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

        string result = string.Empty;
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            result = reader.ReadToEnd();
        }

        return result;
    }
 private static string GetToken() {
        const string ServiceAccountEmail = "someid@developer.gserviceaccount.com";

        var servicio = new PicasaService(null);
        var certificate = new X509Certificate2(HttpContext.Current.Server.MapPath("/key2.p12"), "notasecret", X509KeyStorageFlags.Exportable);

        var serviceAccountCredentialInitializer =
            new ServiceAccountCredential.Initializer(ServiceAccountEmail)
            {
                Scopes = new[] { "https://picasaweb.google.com/data/" }
            }.FromCertificate(certificate);

        var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);

        if (!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
            throw new InvalidOperationException("Access token request failed.");

        return credential.Token.AccessToken;
    }

任何帮助是值得欢迎的!

Any help is welcome!!

推荐答案

你是最好的(恕我直言)的方​​法是忘记库,忘记服务帐户。获取刷新令牌你想插入到谷歌的用户帐户,并使用原始的HTTP REST API调用的Picasa。

You're best (imho) approach would be to forget libraries and forget service accounts. Get a refresh token for the google user account you're trying to insert to, and use the raw HTTP REST API to invoke Picasa.

这篇关于插入图片错误:远程服务器返回错误:(403)禁止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆