获取访问令牌后的Google+个人资料403

时间:2013-09-27 14:08:17

标签: c# oauth-2.0 google-plus

我正在开发一个使用oAuth获取Google+个人资料信息的项目。我尝试了几种不同的方法,并在尝试获取配置文件数据时继续收到403 Forbidden Error Message。

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

     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+服务。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

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

确保您已在“API访问”部分的API控制台中设置了API密钥 Simple API Access

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

Configure Server Key for simple-test

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

如果在切换到使用Server APIKey(您似乎从下面的评论中建议)后错误更改为401错误,则可能是您没有使用客户端的当前Auth令牌。标记在60分钟后到期,因此您需要确保使用当前标记。

相关问题