插入图片错误:远程服务器返回错误:(403)禁止错误、插入图片、服务器

2023-09-05 03:12:22 作者:逃不过心动

我需要你的帮助!

我试着去插入一个新的照片到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://m.xsw88.com/allimgs/daicuo/20230905/887.png.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.