【问题标题】:Google+ Profile 403 after getting access token获取访问令牌后的 Google+ 个人资料 403
【发布时间】:2013-09-28 03:59:15
【问题描述】:

我正在开展一个使用 oAuth 获取 Google+ 个人资料信息的项目。我尝试了几种不同的方法,但在尝试获取配置文件数据时,我不断收到 403 禁止错误消息。

这是我用来获取访问令牌的代码

     GoogleClient googleClient = new GoogleClient
    {
        ClientIdentifier = ConfigurationManager.AppSettings["googleConsumerKey"],
        ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["googleConsumerSecret"]),
    };

    googleClient.RequestUserAuthorization(scope: new[] { "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/plus.me" });

    IAuthorizationState authorization = googleClient.ProcessUserAuthorization();

    if (authorization != null && authorization.AccessToken != null)
    {
        WebRequest testREQ = WebRequest.Create("https://www.googleapis.com/plus/v1/people/me?key=" + ConfigurationManager.AppSettings["APIKey"] + "&access_token=" + Uri.EscapeDataString(authorization.AccessToken));
        WebResponse testRES = testREQ.GetResponse();
    }

上述代码在调用 GetResponse() 时抛出 403

我也试过这里使用的库

http://www.googleplustips.com/resources/3332-NET-Library-Google-APIs-released.aspx

使用此代码

    string profileId = "me";
    string apiKey = ConfigurationManager.AppSettings["APIKey"];
    // You can get an API Key here: https://code.google.com/apis/console#access

    GooglePlusAPIHelper apiHelper = new GooglePlusAPIHelper(profileId, apiKey);

    GPlusActivities activities = apiHelper.ListActivities("eJx9Uj1IAzEUfglXeuASxbWQRX");

    GPlusActivity activity = apiHelper.GetActivity("z13rvzzy2lmtj3p0a22md1c4nyrdufrxv04");

    GPlusActivities activities2 = apiHelper.SearchActivities("Windows 8");

    GPlusPerson person = apiHelper.GetPerson();

    GPlusPeople people = apiHelper.SearchPeople("windows 8 club");

    GPlusComments people2 = apiHelper.ListComments("z120yhh54qrbsn5tr22rtjbiqr3mh1qp504");

    GPlusComment comment = apiHelper.GetComment("2HOqDyvQVCrbz47vK_w9nSrRYnS");

这也会在 ListActivities() 调用中引发 403 Forbidden

我猜这与我未能完成的某些设置有关,但我不知道它可能在哪里。我在我的 API 控制台中选择了所有 Google+ 服务。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# oauth-2.0 google-plus


    【解决方案1】:

    我会验证以确保您使用的 APIKey 是您项目的有效 API 密钥,并且您的请求来自的 IP 地址是您的密钥的有效服务器地址。

    确保您已从 API 控制台的“API 访问”部分设置 API 密钥

    创建服务器密钥时,您需要提供服务器的 IP 地址,Google 将验证该密钥来自授权主机:

    您已表明您使用的是浏览器键而不是服务器键。浏览器键仅供 JavaScript 或在浏览器本身中运行的其他客户端使用,而不是用于可能将其结果输出到浏览器的服务器应用程序。

    如果在您切换到使用服务器 APIKey 后错误变为 401 错误(您似乎从下面的 cmets 建议),则可能是您没有使用来自客户端的当前 Auth Token。令牌在 60 分钟后过期,因此您需要确保使用的是当前令牌。

    【讨论】:

    • 我没有使用服务器密钥,而是使用浏览器密钥(它是一个获取信息的网络应用程序)。我的浏览器应用程序的引用者是我的页面的完整 url 我用我的机器 IP 设置了一个服务器应用程序并获得了 401 Unauthorized,这将是有意义的,因为它是一个网络调用。如果您需要更多信息,请告诉我
    • 如果这是在服务器上运行的 web 应用程序(而不是在客户端上运行的 javascript,这似乎不是),您需要使用服务器 APIKey。问题不在于内容显示在哪里,而在于程序本身在哪里运行,而 C# 通常在服务器上运行。使用错误类型的密钥可能会导致 403 错误。我正在编辑我的问题以处理 401 Unauthorized 问题。
    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2015-05-03
    相关资源
    最近更新 更多