c# - 插入照片错误: The remote server returned an error: (403) Forbidden

标签 c# .net google-oauth picasa

我需要你的帮助!

我正在尝试使用 Oauth 2.0 和简单的 HttpRequest 流程将新照片插入 Picasa 相册中。结果是,按照以下列出的说明操作后,我无法将新照片插入我的 Picasa 网络相册: https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol#Auth

我还必须说,我尝试使用他们提供的 .Net 库,得到了相同的结果。

我现在使用的实现如下:

public static string PostImage(
       string streamImageConvertedToString)
    {
        string url = string.Format("https://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f7f1e7f0ece3efe7c2e5efe3ebeeace1edef" rel="noreferrer noopener nofollow">[email protected]</a>", "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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3908c8e868a87a3878695868f8c938691cd84908691958a80868280808c968d97cd808c8e" rel="noreferrer noopener nofollow">[email protected]</a>";

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

欢迎任何帮助!!

最佳答案

(403) Forbidden

表示您正在尝试使用方法insert这需要授权才能做。

您正在连接到服务帐户 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5968a88808c81a581809380898a958097cb82968097938c86808486868a908b91cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>这应该让您可以访问 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="582b37353d313c183c3d2e3d3437283d2a763f2b3d2a2e313b3d393b3b372d362c763b3735" rel="noreferrer noopener nofollow">[email protected]</a>那时的照片。

您似乎正在尝试访问 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5909680978b848880a58288848c89cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>除非你给了 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4c50525a565b7f5b5a495a53504f5a4d11584c5a4d49565c5a5e5c5c504a514b115c5052" rel="noreferrer noopener nofollow">[email protected]</a>代表访问插入图片<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1c4c2d4c3dfd0dcd4f1d6dcd0d8dd9fd2dedc" rel="noreferrer noopener nofollow">[email protected]</a> (我什至不确定这是否可能)您将不会获得执行此操作的许可。

请记住,服务帐户是 sudo 用户,它有自己的驱动器帐户、日历帐户...它无权访问随机用户数据,除非该用户像其他用户一样授予它们访问权限。

注意:Google .net 客户端库不支持 gdata API。 Picasa 是一个 gdata 库,我喜欢如何尝试合并这两个库,我必须对此进行测试。

关于c# - 插入照片错误: The remote server returned an error: (403) Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30567126/

相关文章:

c# - 使用处理后通过一次性对象的方法返回的对象

c# - Rijndael加密/解密

javascript - CefSharp执行javascript并获取值

c# - Fluent NHibernate,引用硬编码的 C# 方法

c# - 平行发展分支策略

oauth-2.0 - Google API - 来自 Oauth2 的 token 请求返回 “invalid_request”,HTTP 代码为 400

c# - 使用 SQL Server AdoJobStore 配置 Quartz.NET

c# - Windows 服务和 Exchange 服务器之间的通信

python - Google Drive API 权限不足 : Request had insufficient authentication scopes

oauth - Google OAuth2 用于托管在 NAT 后面的 Web 应用程序(没有公共(public) IP 的内网服务器)