从Google Api收到授权码后,我尝试请求检索用户数据,但是总是会出现400错误。
Dim objSerializer = New JavaScriptSerializer()
Dim objContent = New NameValueCollection()
Dim strClientId = "****.apps.googleusercontent.com"
Dim strClientSecret = "****"
Dim strTokenUri = "https://accounts.google.com/o/oauth2/token"
Dim objClient = New WebClient()
objClient.Encoding = Encoding.UTF8
objClient.QueryString.Add("code", strCode)
objClient.QueryString.Add("client_id", strClientId)
objClient.QueryString.Add("client_secret", strClientSecret)
objClient.QueryString.Add("redirect_uri", "http://localhost:38815/Info.aspx")
objClient.QueryString.Add("grant_type", "authorization_code")
Dim data = objClient.UploadValues(strTokenUri, "POST", objClient.QueryString)
Dim objResponse As String = Encoding.UTF8.GetString(data)
Dim objGoogleJwtToken = objSerializer.Deserialize(Of GoogleAccessToken)(objResponse)
错误: System.Net.WebException:远程服务器返回错误:(400)请求不正确。
更新 这是我的帖子数据
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
code=4%2FewGRFvsrdTHGF698QFq6d359KAPRWc4c-8ipKjJU58X1uk14WNc5uRw45N-c88HtbWkL17wEfLlasdfGRSU&
client_id=****.apps.googleusercontent.com&
client_secret=****&
redirect_uri=http%3A%2F%2Flocalhost%3A38815%2FInfo.aspx&
grant_type=authorization_code
我收到以下json结果:
{
"error": "invalid_grant",
"error_description": "Bad Request"
}
答案 0 :(得分:0)
经过一些测试,我发现了另一个与以下主题有关的错误: https://github.com/morgoth/picasa/issues/39
即使更正redirect_uri参数仍未获得经过身份验证的令牌,因此我将Http客户端用于.NET Restsharp,并获得了可行的以下解决方案:
Private Shared Function GetAccessToken3(ByVal strCode As String, ByVal strClientId As String, ByVal strClientSecret As String, ByVal strTokenUri As String) As GoogleAccessToken
Dim objClient As RestClient
Dim objIResponse As IRestResponse
Dim objRequest As RestRequest
Dim objSerializer As New DataContractJsonSerializer(GetType(GoogleAccessToken))
Dim objResponse As GoogleAccessToken
objClient = New RestClient(strTokenUri)
objClient.Authenticator = New HttpBasicAuthenticator(strClientId, strClientSecret)
objRequest = New RestRequest(Method.POST)
objRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded")
objRequest.AddHeader("Accept", "application/json")
objRequest.AddParameter("code", strCode, ParameterType.GetOrPost)
objRequest.AddParameter("client_id", strClientId, ParameterType.GetOrPost)
objRequest.AddParameter("client_secret", strClientSecret, ParameterType.GetOrPost)
objRequest.AddParameter("redirect_uri", "http://localhost:38815/Default.aspx", ParameterType.GetOrPost)
objRequest.AddParameter("grant_type", "authorization_code", ParameterType.GetOrPost)
objIResponse = objClient.Execute(objRequest)
If objIResponse.StatusCode <> HttpStatusCode.OK OrElse objIResponse.ErrorException IsNot Nothing Then
Throw New Exception("Error: " + objIResponse.StatusCode)
End If
objResponse = DirectCast(objSerializer.ReadObject(New MemoryStream(Encoding.UTF8.GetBytes(objIResponse.Content))), GoogleAccessToken)
Return objResponse
End Function
我正在JWT Tool中验证令牌结果