0

我正在尝试从 BOX.COM oauth2 检索访问令牌,无论我做什么,我都会收到相同的错误。{"error":"invalid_request","error_description":"无效的 grant_type 参数或缺少参数"}。我每次尝试都验证了客户端 ID、客户端密码并获得了一个新代码(每 30 秒过期一次)。我已经尝试过 VB.NET、C# 甚至是 FireFox 的 RestClient 插件。下面是我正在使用的 VB.NET 代码。任何帮助将不胜感激!谢谢,布赖恩

Public Sub GetAccessToken(ByVal code As String, ByVal client_id As String, ByVal client_secret As String)
        Dim xrc As RestClient = New RestClient
        Dim grant_type As String = "authorization_code"
        Dim request As New RestRequest(Method.POST)
        Dim strHeaders As String
        Dim response As RestResponse
        Dim strResponse As String


        Try
            'Base URL
            xrc.BaseUrl = "https://api.box.com"

            'Resource
            request.Resource = "oauth2/token"

            'Format Headers
            strHeaders = String.Format("grant_type={0}&code={1}&client_id={2}&client_secret={3}", grant_type, code, client_id, client_secret)

            'Add Headers to request
            request.AddHeader("Authorization", strHeaders)

            'Execute
            response = xrc.Execute(request)

            'Parse Response
            strResponse = response.Content

        Catch ex As Exception

        End Try

    End Sub 
4

2 回答 2

0

这就是问题

        'Format Headers
        strHeaders = String.Format("grant_type={0}&code={1}&client_id={2}&client_secret={3}", grant_type, code, client_id, client_secret)

        'Add Headers to request
        request.AddHeader("Authorization", strHeaders)

您需要将该字符串作为 POST 正文的一部分发送,而不是作为标题发送。

于 2013-05-04T20:02:54.253 回答
0

您可能还需要添加以下内容以确保您使用的是 xml 编码。

request.Method = Method.POST
request.RequestFormat = DataFormat.Xml
于 2013-08-21T21:03:20.920 回答