0

我似乎无法理解我在这里做错了什么。

我下面的代码不断从 youtube 返回 400 代码....

If Not Page.Request.QueryString("code") Is Nothing Then
            Dim code As String = "code=" & Page.Request.QueryString("code") & "&client_id=myclientid&client_secret=mysecret&redirect_uri=http://localhost:61163/Testing/YoutubeAPI.aspx&grant_type=authorization_code"
        Dim request As HttpWebRequest = WebRequest.Create("https://accounts.google.com/o/oauth2/token")
        Dim byteData As Byte() = Encoding.UTF8.GetBytes(code)
        With request
            .Method = "POST"
            .ContentType = "application/x-www-form-urlencoded"

            .ContentLength = byteData.Length
        End With

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

        Dim WebResponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim responseStream As Stream = WebResponse.GetResponseStream()
        Dim sb As StringBuilder = New StringBuilder
        Using reader As New StreamReader(responseStream, System.Text.Encoding.UTF8)
            Dim line As String = reader.ReadLine()
            If Not line Is Nothing Then
                sb.Append(line)
            End If
        End Using


    End If

错误发生在 request.GetRequestStream()... 在这个阶段我能弄清楚的最好的事情是谷歌不喜欢我所要求的但似乎无法找出原因?

(我的客户ID和秘密被无意中交换了......)

4

1 回答 1

0

好吧,我不确定在回答我自己的问题时礼仪是否正确,但您可能会发现它很有用。

答案在于内容字符串的 URL 编码...

所以现在稍微修改一下,释放了 OAuth2 的强大功能,我可以从 youtube API 获得几乎任何我想要的东西 :) (尽管不感谢谷歌文档的黑洞兔子洞......

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.Request.QueryString("code") Is Nothing Then
        Dim Token As String = CodeTrade("code=" & Server.UrlEncode(Page.Request.QueryString("code")) & "&redirect_uri=" & Server.UrlEncode("http://localhost:61163/Testing/YoutubeAPI.aspx") & "&client_id=xxx=&client_secret=xxx&grant_type=authorization_code")

        'now i CAN do something with the magical and elusive access_token from this point forward....
    End If
End Sub
Public Shared Function CodeTrade(code As String) As String
    Dim apiResponse As String

    Dim postData As String = code

    Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://accounts.google.com/o/oauth2/token"), HttpWebRequest)

    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"

    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

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

    Dim response As WebResponse = request.GetResponse()

    apiResponse = DirectCast(response, HttpWebResponse).StatusDescription.ToString()

    dataStream = response.GetResponseStream()

    Dim reader As New StreamReader(dataStream)

    Dim responseFromServer As String = reader.ReadToEnd()

    reader.Close()
    dataStream.Close()
    response.Close()

    Return responseFromServer
End Function
于 2013-05-31T06:46:01.527 回答