【问题标题】:Bad Request 400 when making a token request to Google gmail API向 Google gmail API 发出令牌请求时出现错误请求 400
【发布时间】:2015-05-20 05:18:51
【问题描述】:

我按照这个文档提出令牌请求https://developers.google.com/identity/protocols/OAuth2InstalledApp

在第一步中,它工作正常。我可以得到一个验证码。 在第二步中,我遇到了 400 Bad Request 的问题。我已经为这个问题寻找答案两天了,但我无法解决这个问题。 像文档一样设置所有属性,不过没关系:

POST /oauth2/v3/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded

code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
client_id=8819981768.apps.googleusercontent.com&
client_secret=your_client_secret&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code

这是我的代码:

    postData.Clear()
    ' code is the authentication code in the first request
    postData.Add("code=" + code)
    postData.Add("client_id=###############.apps.googleusercontent.com")
    postData.Add("client_secrect=####################")
    postData.Add("redirect_uri=urn:ietf:wg:oauth:2.0:oob")
    postData.Add("grant_type=authorization_code")

    Dim data As String = String.Join("&", postData.ToArray())
    Dim request As HttpWebRequest = HttpWebRequest.Create("https://www.googleapis.com/oauth2/v3/token")
    Dim byteData() As Byte = Encoding.UTF8.GetBytes(data)

    request.Host = "www.googleapis.com"
    request.Method = WebRequestMethods.Http.Post
    request.ProtocolVersion = HttpVersion.Version11
    request.ContentType = "application/x-www-form-urlencoded"
    request.ContentLength = byteData.Length

    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteData, 0, byteData.Length)
    dataStream.Close()        

    Dim response As HttpWebResponse = request.GetResponse()
    Dim reader As Stream = response.GetResponseStream()
    response.Close()

感谢您的回答!

【问题讨论】:

    标签: vb.net oauth google-api token bad-request


    【解决方案1】:

    我刚刚发现了一个错误。这是一个愚蠢的错误。我输入了client_secret 的client_secrect inteads。我不敢相信我花了 2 天时间来修复这个错误。

    【讨论】:

      猜你喜欢
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 2018-06-09
      • 2019-03-13
      • 2018-10-28
      • 2021-12-13
      相关资源
      最近更新 更多