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

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

问题描述

我在使用新发布的 Google+ API 检索访问令牌时遇到了一些问题...

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

我一直在关注文档,我得到了一个代码(4/blablabla"),但是当我发送 POST 请求以获取访问令牌时,我得到一个(400)错误请求"响应......

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...

这是我的一段代码:

// 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 ...

推荐答案

您忘记了 POST 请求.试试这个:

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 ...

这会给你一个带有访问令牌的 Json.您还可以看到我的问题,我今天问了这个问题,后来解决了.

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天全站免登陆