2011-09-23 74 views
1

struggling to authenticate在Google+ API后,我终于成功找回我的信息与https://www.googleapis.com/plus/v1/people/ {MyUserId}?关键= {ApiKey}Google+的API让人们“我”

然而,尽管我没有获得与访问令牌范围https://www.googleapis.com/auth/plus.me我不能请求“我”:https://www.googleapis.com/plus/v1/people/me?key= {ApiKey}

我最终得到401未经授权的请求......这是迟到了,我没有得到我想念的东西。

这里是我的代码:

 string apiUrl = "https://www.googleapis.com/plus/v1/people/me?key={my api key"; 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl); 
     request.Method = HttpMethod.GET.ToString(); 

     try 
     { 
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       => 401 error 

谢谢您的帮助!

+0

尝试排除API密钥,一旦您获得了OAuth令牌 - [People:get](http://developers.google.com/+/api/latest/people/get#examples) _:“”如果使用userId值“me”,则此方法需要使用已授予OAuth范围的令牌进行认证“”._ [OAuth](http://developers.google.com/+/api/oauth ):_“”如果请求需要授权(例如对个人私人数据的请求),则它必须包含OAuth 2.0令牌。它也可能包含API密钥,但它不一定要。“”_ – Smudge202

+1

您需要像这样请求:'“https://www.googleapis.com/plus/v1/people/me?key={api key}&access_token = {access token}“' –

回答

0

我已经3年了,但是我在找到一个几乎完全相同的问题的解决方案时发现了这篇文章。然而,我试图避免使用Google的API,因此我想我会发布自己的解决方案,以防有人想要在没有Google API的情况下看到这种方式。我正在使用RestSharp处理HTTP请求,但这不是必需的。

//craft the request 
string requestUrl = "https://www.googleapis.com/plus/v1/people/me?access_token=" + AccessToken; 
RestClient rc = new RestClient(); 
RestRequest request = new RestRequest(requestUrl, Method.GET); 
request.AddHeader("Content-Type", "application/json"); 
request.AddHeader("x-li-format", "json"); 
request.RequestFormat = DataFormat.Json; 

//send the request, and check the response. 
RestResponse restResponse = (RestResponse)rc.Execute(request); 
if (!restResponse.ResponseStatus.Equals(ResponseStatus.Completed)) { return null; } 

与问题中原始实现的主要区别在于我没有使用RestSharp - 这是不重要的。主要区别在于我在查询字符串中传递了access_token而不是密钥。

相关问题