如何获得谷歌的OAuth访问令牌? [英] How to get access token for google oauth?

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

问题描述

我使用C#(ASP.NET)。我想使用谷歌的OAuth在我的应用程序访问用户配置文件的细节。我顺利拿到了授权code,但有问题,在获取访问令牌。
我preFER的谷歌教程的。在教程中,我读,我要发送请求,并从谷歌的反应。对于我使用 System.Net.HttpWebRequest / HttpWebResponse (我是在正确的方式去)。我用这个code ....

I am using C# (ASP.NET). I want to use Google oauth for accessing the user profile detail in my app. I successfully got the authorization code but having problem in getting the access token. I prefer the Google tutorials. In tutorial, I read that I have to send the request and get the response from google. For that I use System.Net.HttpWebRequest/HttpWebResponse (is I going in a right way). I use this code....

byte[] buffer = Encoding.ASCII.GetBytes("?code=" + code + "&client_id=xxx&client_secret=xxx&redirect_uri=xxxx&grant_type=authorization_code");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://accounts.google.com");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;

Stream strm = req.GetRequestStream();
strm.Write(buffer, 0, buffer.Length);
strm.Close();

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Response.Write(((HttpWebResponse)resp).StatusDescription);

不过,我得到了错误:

But, I got the error:

远程服务器返回错误:(405)不允许的方法

The remote server returned an error: (405) Method Not Allowed.

更新:这里变量 code 是授权code

Update: Here variable code is authorization code.

推荐答案

我想您发送POST请求到错误的端点,正确的是 https://accounts.google.com/ O /的oauth2 /令牌

I think you are sending the POST request to the wrong endpoint, the correct one is https://accounts.google.com/o/oauth2/token

这篇关于如何获得谷歌的OAuth访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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