开发者

Access token Google+

开发者 https://www.devze.com 2023-04-06 23:36 出处:网络
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

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 ...
开发者_StackOverflow


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.


The Google+ API uses OAuth 2.0 for authorization. You seem to be implementing a mix of OAuth 2.0 and OAuth 1.0: your code calculates an OAuth 1.0 oauth_signature for the OAuth 2.0 request which is obsolete in OAuth 2.0.

Take a look at the OAuth 2.0 specification draft and try to follow the example in Google's OAuth 2.0 documentation exactly. Or just use Google's .NET library which comes with OAuth 2.0 support.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号