访问令牌的Google+ [英] Access token Google+

查看:189
本文介绍了访问令牌的Google+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到新发布的Google+ API的几个问题来获取一个访问令牌...

我一直文档以下,我得到了code(4 / blablabla),但是当我发送POST请求获得一个访问令牌,我得到了(400)错误的请求的响应...

下面是我的一块code的:

  //我们有一个请求code,现在我们要访问令牌
StringBuilder的authLink =新的StringBuilder();
authLink.Append(https://accounts.google.com/o/oauth2/token);
authLink.AppendFormat(code = {0}?,gplus code);
authLink.AppendFormat(与& CLIENT_ID = {0},myClientId);
authLink.AppendFormat(与& client_secret = {0},mySecret);
authLink.AppendFormat(与& REDIRECT_URI = {0},myRedirectUri);
authLink.Append(与&范围= HTTPS://www.googleapis.com/auth/plus.me);
authLink.Append(与& grant_type = authorization_ code);OAuthBase的OAuth =新OAuthBase();
串normalizedUrl,normalizedRequestParameters;
字符串oAuthSignature = oAuth.GenerateSignature(新的URI(authLink.ToString()),APPKEY,appSecret,code,空,HttpMethod.POST.ToString(),oAuth.GenerateTimeStamp(),oAuth.GenerateNonce(),OAuthBase。 SignatureTypes.HMACSHA1,出normalizedUrl,出normalizedRequestParameters);
oAuthSignature = oAuth.UrlEn code(oAuthSignature);//重建与授权查询网址
字符串的URL =的String.Format({0} {1}&安培; oauth_signature = {2},normalizedUrl,normalizedRequestParameters,oAuthSignature);HttpWebRequest的请求=(HttpWebRequest的)WebRequest.Create(url.ToString());
request.Method = HttpMethod.POST.ToString();
request.ContentType =应用/的X WWW的形式urlen codeD;
request.ContentLength = 0;//发送请求并获得响应
尝试
{
    使用(HttpWebResponse响应=(HttpWebResponse)request.GetResponse())
    {
        // 做东西 ...


解决方案

您忘了POST请求。试试这个:

 字符串的URL =htt​​ps://accounts.google.com/o/oauth2/token;HttpWebRequest的请求=(HttpWebRequest的)WebRequest.Create(url.ToString());
request.Method = HttpMethod.POST.ToString();
request.ContentType =应用/的X WWW的形式urlen codeD;//你每亩得到任何响应之前做POST请求
UTF8Encoding utfenc =新UTF8Encoding();
字节[]字节= utfenc.GetBytes(参数); //参数=code = ...&安培; CLIENT_ID = ...;
流媒体操作系统= NULL;
尝试//帖子发送
{
    webRequest.ContentLength = bytes.Length; //计数要发送的字节
    OS = webRequest.GetRequestStream();
    os.Write(字节,0,bytes.Length); // 发送
}尝试
{
    使用(HttpWebResponse响应=(HttpWebResponse)request.GetResponse())
    {
        // 做东西 ...

这会给你一个JSON的访问令牌。你还可以看到我的问题,我今天问,后来解决了。

I'm experiencing few issues with the new-released Google+ API to retrieve an access token...

I have been following the documentation, I got a Code ("4/blablabla") but when I send the POST request to get an access token, I get a "(400) Bad Request" response...

Here is my piece of code :

// We have a request code, now we want an access token
StringBuilder authLink = new StringBuilder();
authLink.Append("https://accounts.google.com/o/oauth2/token");
authLink.AppendFormat("?code={0}", gplusCode);
authLink.AppendFormat("&client_id={0}", myClientId);
authLink.AppendFormat("&client_secret={0}", mySecret);
authLink.AppendFormat("&redirect_uri={0}", myRedirectUri);
authLink.Append("&scope=https://www.googleapis.com/auth/plus.me");
authLink.Append("&grant_type=authorization_code");

OAuthBase oAuth = new OAuthBase();
string normalizedUrl, normalizedRequestParameters;
string oAuthSignature = oAuth.GenerateSignature(new Uri(authLink.ToString()), appKey, appSecret, code, null, HttpMethod.POST.ToString(), oAuth.GenerateTimeStamp(), oAuth.GenerateNonce(), OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters);
oAuthSignature = oAuth.UrlEncode(oAuthSignature);

// Rebuild query url with authorization
string url = string.Format("{0}?{1}&oauth_signature={2}", normalizedUrl, normalizedRequestParameters, oAuthSignature);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
request.Method = HttpMethod.POST.ToString();
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = 0;

// Send the request and get the response
try
{
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        // Do stuff ...

解决方案

You forgot the POST request. Try this:

string url = "https://accounts.google.com/o/oauth2/token";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
request.Method = HttpMethod.POST.ToString();
request.ContentType = "application/x-www-form-urlencoded";

// You mus do the POST request before getting any response
UTF8Encoding utfenc = new UTF8Encoding();
byte[] bytes = utfenc.GetBytes(parameters); // parameters="code=...&client_id=...";
Stream os = null;
try // send the post
{
    webRequest.ContentLength = bytes.Length; // Count bytes to send
    os = webRequest.GetRequestStream();
    os.Write(bytes, 0, bytes.Length);        // Send it
}

try
{
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        // Do stuff ...

This will give you a Json with the access token. Also you can see my question, that I asked today and solved later.

这篇关于访问令牌的Google+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆