获取承载令牌请求

时间:2019-06-17 13:10:02

标签: .net api oauth-2.0

对象是从请求的API接收承载令牌。我可以使用邮递员完成此操作,但想在我的应用程序中实现。

我尝试了许多不同的通话方式,但是这似乎与我最接近。我目前正在获取状态码:使用以下脚本时出现400错误的请求。

有人有解决此问题的想法吗?

Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(user & ":" + password))

Dim request As HttpWebRequest = HttpWebRequest.Create("https://oauth2.url.com/token")
        request.Proxy = Nothing
        request.Method = "GET"
        request.ContentType = "application/json"
        request.Headers.Add("client_id", "clientid")
        request.Headers.Add("client_secret", "clientsecret")
        request.Headers.Add("grant_type", "authorization_code")
        request.Headers.Add("auth_url", "https://oauth2.url.com/authorization")
        request.Headers(HttpRequestHeader.Authorization) = "Basic " & credentials
        request.Headers.Add("redirect_uri", "http://localhost:5000/auth/callback")

        'create the response and reader
        Dim Response As HttpWebResponse = request.GetResponse
        Dim ResponseStream As System.IO.Stream = Response.GetResponseStream

        'Create Stream reader
        Dim StreamReader As New System.IO.StreamReader(ResponseStream)
        Dim data As String = StreamReader.ReadToEnd
        StreamReader.Close()

        'get the data
        Dim jsonString = data.ToString.Replace("'", "''")

1 个答案:

答案 0 :(得分:0)

this页的示例中可以看到,必须使用POST而不是GET。 另一个问题是,您正在将所有参数发送到HTTP请求的标头中,而不是简单地将它们放在请求的正文中。