【问题标题】:vb.net Google api for device authentication simple http post request fail用于设备身份验证的 vb.net Google api 简单的 http post 请求失败
【发布时间】:2012-11-05 06:46:58
【问题描述】:

我正在创建一个谷歌驱动设备应用程序。 我正在尝试完成本教程: https://developers.google.com/accounts/docs/OAuth2ForDevices

我第一时间就有货了 简单的 POST 步骤

这会返回 400 错误请求:

  Try

        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim data As StringBuilder
        Dim byteData() As Byte
        Dim postStream As Stream = Nothing
        request = DirectCast(WebRequest.Create("https://accounts.google.com/o/oauth2/device/code"), HttpWebRequest)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        data = New StringBuilder()
        data.Append("client_id=XXXXXXXXXXX.apps.googleusercontent.com&")
        data.Append("scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive")
        byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
        request.ContentLength = byteData.Length
        Try
            postStream = request.GetRequestStream()
            postStream.Write(byteData, 0, byteData.Length)
        Finally
            If Not postStream Is Nothing Then postStream.Close()
        End Try

        Try
            ' Get response  
            response = DirectCast(request.GetResponse(), HttpWebResponse)

            ' Get the response stream into a reader  
            reader = New StreamReader(response.GetResponseStream())

            ' Console application output  
            tb1.Text = reader.ReadToEnd()
        Finally
            If Not response Is Nothing Then response.Close()
        End Try

    Catch ex As Exception
        tb1.Text = ex.Message
    End Try

我是否缺少发布信息?是 httpwebrequest 不是正确的工具吗? 谢谢

【问题讨论】:

    标签: google-drive-api google-authentication google-api-dotnet-client


    【解决方案1】:

    您不必编写自己的授权,您可以使用 .NET 客户端库。我们有用于 c# 的 examples here,您应该能够轻松地将它们转换为 VB.NET。

    【讨论】:

    • 问题在于,这是针对访问受限的应用程序设备,身份验证 url 必须在另一台使用“google.com/device”的计算机上完成,用户输入他给出的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多