问题 如何获取谷歌oauth的访问令牌?


我正在使用C#(ASP.NET)。我想使用Google oauth访问我的应用中的用户个人资料详情。我成功获得了授权码,但在获取访问令牌时遇到了问题。 我更喜欢 谷歌教程。在教程中,我读到我必须发送请求并从谷歌获得响应。为此,我使用 System.Net.HttpWebRequest/HttpWebResponse (我是以正确的方式前进)。我用这个代码....

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);

但是,我收到了错误:

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

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


2532
2017-08-06 17:46


起源

你为什么不用 code.google.com/p/google-api-dotnet-client/wiki/OAuth2 - user854301
@ user854301我可以参考这个,但我想知道使用的 HttpWebRequest/Response  是对还是不对?我可以将请求发送到谷歌吗? HttpWebRequest。 - Sagar
什么是你的缓冲区中的“代码”? - Apoorva
@Apoorva这是授权码。 - Sagar
如何获得授权码...你能否告诉我我不知道它...


答案:


我认为你正在向错误的端点发送POST请求,正确的是 https://accounts.google.com/o/oauth2/token


8
2017-08-06 20:05



我用它,但它显示了其他错误。现在错误是 The remote server returned an error: (400) Bad Request. 我哪里错了? - Sagar
深入了解异常的详细信息,完整的错误消息将告诉您您的请求有什么问题 - Claudio Cherubino


由于我在实施Google身份验证的过程中遇到了类似的问题,我将发布有效的代码..最后提到的问题:错误(400)错误的请求可能是由导致'?'引起的在上面的代码..

 string codeClient = "code="+ t +"&client_id=number.apps.googleusercontent.com&";
 string secretUri = "client_secret=yoursecret&" + "redirect_uri=path&"
      + "grant_type=authorization_code";
 postString = codeClient + secretUri;

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

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

 UTF8Encoding utfenc = new UTF8Encoding();
 byte[] bytes = utfenc.GetBytes(postString);
 Stream os = null;
 try
 {
      request.ContentLength = bytes.Length;
      os = request.GetRequestStream();
      os.Write(bytes, 0, bytes.Length);
 }
 catch
 { }

 try
 {
      HttpWebResponse webResponse = (HttpWebResponse) request.GetResponse();
      Stream responseStream = webResponse.GetResponseStream();
      StreamReader responseStreamReader = new StreamReader(responseStream);
      result = responseStreamReader.ReadToEnd();//parse token from result

4
2017-09-18 20:05



什么是“t”用于代码..以及如何得到它..
初始授权码... - Martin Pfeffer
它仍然使用此代码说Bad Request - Sahil Bhatia
@Sahil,确保oauth url是有效的。请注意,我差不多5年前使用过此代码段。 - Neno
谢谢,现在看起来工作正常。 - Sahil Bhatia


我的代码正在运行,我在上面的两行中犯了错误。它应该是这样的

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("https://accounts.google.com/o/oauth2/token");

剩下的代码是正确的。


2
2017-09-21 08:08



代码中的Code变量是什么.. - Devi Prasad
@Viktor:我注意到你的 客户ID 和 客户秘密 都包含在缓冲区中。我托管的网站没有SSL。您知道Google是否会接受来自非SSL连接的网络请求吗?显然,它不安全,有人可以狙击我的凭据,但我不知道解决方法。 - jp2code


原始请求似乎有点过时了。但我发现Google的代码示例中包含许多“最佳实践”内务代码,这些代码很难与基本操作分开。

我最近发布了一个文档,它将所有REST操作表示为curl命令。在每种语言中都很难熟悉,但卷曲似乎是普遍的。大多数人都知道 - 否则,它很容易掌握。在我的卷曲例子中, -d flag表示POST操作。否则,参数将附加到URL。

http://www.tqis.com/eloquency/googlecalendar.htm


0
2018-01-05 20:17





public string ReceiveTokenGmail(string code, string GoogleWebAppClientID, string GoogleWebAppClientSecret, string RedirectUrl)
{
    string postString = "code=" + code + "&client_id=" + GoogleWebAppClientID + @"&client_secret=" + GoogleWebAppClientSecret  + "&redirect_uri=" + RedirectUrl;

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

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

    UTF8Encoding utfenc = new UTF8Encoding();
    byte[] bytes = utfenc.GetBytes(postString);
    Stream os = null;
    try
    {
        request.ContentLength = bytes.Length;
        os = request.GetRequestStream();
        os.Write(bytes, 0, bytes.Length);
    }
    catch
    { }
    string result = "";

    HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
    Stream responseStream = webResponse.GetResponseStream();
    StreamReader responseStreamReader = new StreamReader(responseStream);
    result = responseStreamReader.ReadToEnd();

    return result;
}

0
2017-07-04 18:44



如何从谷歌获取身份验证代码?我想使用你的代码,所以我不必使用key.json文件,只需使用app配置我的客户端ID和客户端密码。如果你可以分享如何获取授权代码,那将是非常棒的。 - CyberNinja