【问题标题】:How should this HTTP request for oAuth token be formatted?这个 oAuth 令牌的 HTTP 请求应该如何格式化?
【发布时间】:2019-09-13 13:30:16
【问题描述】:

在 VB .net 环境中,我正在进行以下调用,尝试实现 OAuth 流程的授权步骤以连接到 Accelo API(时间输入和计费类型的应用程序)。我正在尝试获取访问令牌:

Dim jsonstring = "{'Content-Type':'Application/x-www-Form-urlencoded', 
'authorization':'Basic MDBhM...GbG5oLlZB'}"
Dim data = Encoding.UTF8.GetBytes(jsonstring)
Dim result_post = SendRequest(New Uri("https://ourinfo.api.accelo.com/oauth2/v0/token"), data, "application/json", "POST")

函数定义如下:

Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
    Dim req As WebRequest = WebRequest.Create(uri)
    req.ContentType = contentType
    req.Method = method
    req.ContentLength = jsonDataBytes.Length


    Dim stream = req.GetRequestStream()
    stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
    'stream.Close()

    Dim response = req.GetResponse().GetResponseStream()

    Dim reader As New StreamReader(response)
    Dim res = reader.ReadToEnd()
    reader.Close()
    response.Close()

    Return res
End Function

我一直在这一行收到错误:

Dim response = req.GetResponse().GetResponseStream()

 "System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'"

在我看来,这像是一个语法错误,或者我的调用方法的形成方式或传递的参数格式。我从这里得到了 HTTP 请求代码建议/格式:

How to POST a JSON to a specific url using VB.NET?

我正在使用 Accelo API 设置内容类型和授权“基本”部分,其中字符串以 Base 64 编码。这是一个服务应用程序,所以我应该能够获取令牌1 步(无需用户确认)。我注册时已经有一个“令牌”,但 API 仍然指示我应该执行此代码。我正在关注这个:

https://api.accelo.com/docs/?_ga=2.218971609.1390377756.1568376911-2053161277.1565440093#service-applications

谁能告诉我我到底做错了什么?我很困惑,这是我第一次尝试实现 OAuth。

API 文档中有一些示例代码如下所示:

POST /oauth2/v0/token HTTP/1.1
Host: planet-express.api.accelo.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic {client_credentials}

grant_type=client_credentials
scope=read(staff)

我不确定 = 和 : 语法用途之间的区别。我无法搜索并找到任何关于我是否正确调用所有内容的答案。我应该在 JSON 字符串中传递范围和 grant_type,还是将其设置为“SendRequest”函数中“req”对象的属性?我知道grant_type 应该是必需的,但我该如何设置呢?

如果我应该以这种方式获得令牌,我最初注册时收到的令牌是什么?

【问题讨论】:

  • Content Type 和 Authorization 应该在 http 标头中,而不是正文中(尽管 Content Type 有一个枚举属性)。
  • 好的,先生,谢谢。我意识到我的整个脚本格式错误,因为我完全误解了调用方法。我误解了标题与数据。

标签: json vb.net http oauth


【解决方案1】:

我在一位同事的帮助下完成了这项工作。显然我混淆了标题数据和json“数据”,而且我也没有正确格式化数据。我也应该查看 WebResponse,而不仅仅是 WebRequest。我将代码更改为以下代码,现在可以使用:

Sub SendRequestGetAccess()
    Dim req As WebRequest = WebRequest.Create(uri)
    req.Method = "POST"
    req.Headers.Add("Authorization", "Basic " & "MDB....")
    req.ContentType = "Application/x-www-Form-urlencoded"
    req.ContentLength = jsonDataBytes.Length
    Dim stream = req.GetRequestStream()
    stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
    stream.Close()

    Dim response As WebResponse = req.GetResponse()
    Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
    Dim dataStream = response.GetResponseStream()
    Dim reader As StreamReader = New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    Console.WriteLine(responseFromServer)
    reader.Close()
    dataStream.Close()
    response.Close()
    'Dim firstItem = jsonResult.Item("data").Item(0).Value(Of String)("token")
    Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(responseFromServer)
    Dim _itemvalue = j("itemkey")

End Sub

我不得不在我的 json 数据中使用破折号而不是冒号:

 Dim jsonstring = "grant=creds" & "&scope=read"
 Dim data = Encoding.UTF8.GetBytes(jsonstring)
 SendRequestGetAccess(New Uri("https...site.com/oauth2/v0/token"), data)

【讨论】:

    猜你喜欢
    • 2011-09-18
    • 1970-01-01
    • 2012-06-22
    • 2012-08-13
    • 2011-09-05
    • 2017-10-26
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多