BOX访问令牌API V2

时间:2013-01-25 19:13:55

标签: .net vb.net api token box-api

我正在尝试从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 

2 个答案:

答案 0 :(得分: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正文的一部分发送,而不是作为标题发送。

答案 1 :(得分:0)

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

request.Method = Method.POST
request.RequestFormat = DataFormat.Xml