vb.net - Google OAuth token 错误 - 400 错误请求

标签 vb.net google-analytics google-oauth google-analytics-api

我正在尝试使用 OAuth2 和“已安装的应用程序”流程(获取身份验证代码,然后请求 token )对我的应用程序进行身份验证。我收到了 400 bad request error在 GetResponse() 行上请求 token 时。我的代码如下:

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但还没有找到适合此代码的解决方案。我已经检查了所有建议的解决方案并实现了它们,但到目前为止还没有运气。

最佳答案

看起来您没有为参数设置值 授权码 , client_id client_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                                    

关于vb.net - Google OAuth token 错误 - 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17793356/

相关文章:

c# - 将事件从 C# 转换为 VB.Net...如何?

vb.net - com类库注册

ios - 无法导入 pod Google/Analytics 的框架目标

php - 在函数外返回值

node.js - 在 Heroku 中部署时,Google 回调 url 返回 400

google-calendar-api - 开发人员控制台上的 Google Calendar Api 错误请求 400 事件

api - Google API HTTP 401 - token 无效 - AuthSub token 的范围错误

javascript - 添加/删除旋转 gif 无限滚动?

MySQL 远程连接找不到特定主机

google-analytics - 发送带有Google Analytics(分析)事件的日期