0

我有这个代码。它返回错误 400 错误请求,我找不到原因。

第 31 行的错误(这是它应该得到响应的地方......

  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 = GetToken("code=" & Server.UrlEncode(Page.Request.QueryString("code")) & "&client_id=xxx&client_secret=xxx&grant_type=authorization_code&redirect_uri=" & Server.UrlEncode("http://localhost:61163/Testing/YoutubeAPI.aspx"))
        'do something with the magical and elusive access_token from this point forward....
    End If
End Sub
Public Shared Function GetToken(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
4

2 回答 2

0

我不确定您的代码,但您始终可以使用YouTube Dotnet 客户端库

这是一个使用它的示例应用程序

于 2013-05-30T22:56:14.657 回答
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:49:16.903 回答