【发布时间】:2022-09-28 04:04:23
【问题描述】:
我正在尝试使用 ClientID 和 SecretID 获取 OAuth 令牌。
到目前为止我的代码:
Dim clientId As String = \"8cd6b80dd822961f362\"
Dim clientSecret As String = \"5afbd4bb280f29cba5ec1f362\"
Dim credentials = String.Format(\"{0}:{1}\", clientId, clientSecret)
Dim headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials))
Dim content = New FormUrlEncodedContent(New Dictionary(Of String, String) From {
{\"client_id\", clientId},
{\"client_secret\", clientSecret},
{\"response_type\", \"code\"},
{\"redirect_uri\", \"https://somesite.com/\"},
{\"grant_type\", \"authorization_code\"}})
Dim requestMessage = New HttpRequestMessage(HttpMethod.Post, \"https://api.site.com/oauth2/authorize\")
requestMessage.Headers.Authorization = New AuthenticationHeaderValue(\"Basic\", headerValue)
requestMessage.Content = content
Dim client As HttpClient = New HttpClient()
Dim task = client.SendAsync(requestMessage)
Dim response = task.Result
response.EnsureSuccessStatusCode()
Dim responseBody As String = response.Content.ReadAsStringAsync().Result
MsgBox(responseBody)
上面的代码返回 redirect_uri 站点的 HTML 而不是令牌。
我错过了什么或做错了什么?
使用邮递员和提供的凭据,我设法获得了令牌。
-
您能否从邮递员那里执行成功身份验证的网络跟踪,并通过重定向和状态码共享不同的请求?您能否也添加您的 VB 进程停止的点?
-
当我使用 Postman 获取令牌时,它会打开一个浏览器窗口并提示我在继续之前从商店中选择一个证书。如何在我的代码中复制它?我认为这是我缺少的部分:选择要与请求一起发送的证书。
-
您使用的是智能卡读卡器之类的东西还是证书存储在哪里?
-
我有一个安装了证书的数字签名 USB 令牌。
标签: vb.net oauth-2.0 dotnet-httpclient