2013-07-22 165 views
6

我試圖使用OAuth2驗證我的應用程序並使用'安裝的應用程序'流(獲取授權碼,然後請求令牌)。在GetResponse()行上請求令牌時,我得到一個400 bad request error。我的代碼如下:Google OAuth令牌錯誤 - 400錯誤請求

Public Sub New() 
    Dim tokenRequest As WebRequest = 
     WebRequest.Create("https://accounts.google.com/o/oauth2/token") 

    Dim requestString As String = "code=<auth-code>" _ 
         & "&client_id=<client_id>" _ 
         & "&client_secret=<client_secret>" _ 
         & "&redirect_uri=http://localhost" _ 
         & "&grant_type=authorization_code" 

    byteArray = StrToByteArray(System.Web.HttpUtility.UrlEncode(requestString)) 

    tokenRequest.Credentials = CredentialCache.DefaultCredentials 
    tokenRequest.Method = "POST" 
    tokenRequest.ContentLength = byteArray.Length 
    tokenRequest.ContentType = "application/x-www-form-urlencoded" 
    Dim dataStream As Stream = tokenRequest.GetRequestStream() 

    dataStream.Write(byteArray, 0, byteArray.Length) 
    dataStream.Close() 

    Console.WriteLine("Getting response...") 

    'Get response 
    Try 
     Dim response As WebResponse = tokenRequest.GetResponse() 

     Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) 

     Dim data As Stream = response.GetResponseStream 

     Array.Resize(byteArray, 4096) 

     Array.Clear(byteArray, 0, byteArray.Length) 

     data.Read(byteArray, 0, byteArray.Length) 

     response.Close() 

    Catch wex As WebException 
     Console.WriteLine("ERROR! : ") 
     Console.WriteLine(wex.Message) 
     Console.WriteLine(wex.Status) 
     Console.WriteLine(wex.Data) 
     Console.WriteLine(wex.InnerException.Message) 
     Console.WriteLine(wex.HelpLink) 
    End Try 
End Sub 

錯誤的細節都低於:

The remote server returned an error: (400) Bad Request. 
7 
System.Collections.ListDictionaryInternal 
System.NullReferenceException: Object reference not set to an instance of an obj 
ect. 
    at GADownload.GoogleAnalytics..ctor() in ***.vb:line 86 
    at GADownload.Main1.Main(String[] args) in ****.vb:line 18 

我看了一下Google GetAccessToken : Bad Request 400Google GData .Net OAuthUtil.GetAccessToken 400 Bad Request但還沒有找到適合於這個代碼的解決方案。我已經檢查了所有建議的解決方案並實施了它們,但目前爲止沒有運氣。

回答

0

看起來你沒有爲參數AUTH碼設定值,CLIENT_IDclient_secret

您可以使用curl命令調試這些參數,以查看這是否是問題的根源。例如

curl -X POST -d "code=<auth-code>&client_id=<client_id>&client_secret=<client_secret>"&grant_type=authorization_code" http://localhost:8000/auth/token          
0

你可以嘗試URL編碼REDIRECT_URI REDIRECT_URI = http://localhost

這就是我看到您的代碼與我的唯一。這裏是我的代碼,這是類似於VB和正在工作

Dim sb As New StringBuilder 
sb.Append("code=").Append(Request.QueryString("code")) _ 
.Append("&client_id=") _ 
.Append(Session.Item("ClientID")) _ 
.Append("&client_secret=") _ 
.Append(Session.Item("ClientSecret")) _ 
.Append("&redirect_uri=") _ 
.Append(HttpUtility.UrlEncode("http://localhost/1.aspx")) _ 
.Append("&grant_type=authorization_code") 
Dim requestGoogle As HttpWebRequest = 
WebRequest.Create("https://accounts.google.com/o/oauth2/token") 
requestGoogle.Method = "POST" 
requestGoogle.ContentType = "application/x-www-form-urlencoded" 
requestGoogle.ContentLength = sb.Length 
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(sb.ToString) 
sb.Clear() 
requestGoogle.GetRequestStream.Write(byteArray, 0, byteArray.Length) 
byteArray = Nothing 
Dim responseGoogle As HttpWebResponse = requestGoogle.GetResponse() 
If responseGoogle.StatusCode = HttpStatusCode.OK Then 
    Dim sr As StreamReader = _ 
    New StreamReader(responseGoogle.GetResponseStream) 
    Dim s As String = sr.ReadToEnd 
    sr.Close() 
    responseGoogle.GetResponseStream.Close() 
    requestGoogle.GetRequestStream.Close() 
    'Response.Write(s) 
End If 
相關問題