谷歌oauth请求中的错误400

时间:2013-05-30 22:03:16

标签: asp.net youtube-api oauth-2.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

2 个答案:

答案 0 :(得分:0)

我不确定您的代码,但您始终可以使用YouTube Dotnet client libraries

这是使用它的sample application

答案 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