QuickBooks Online Api错误“远程服务器返回错误:(400)错误的请求". [英] QuickBooks Online Api Error "The remote server returned an error: (400) Bad Request"

查看:82
本文介绍了QuickBooks Online Api错误“远程服务器返回错误:(400)错误的请求".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QBO API,并且正在尝试检索访问和刷新令牌的步骤.当我发送请求时,尝试获取指出远程服务器返回错误:(400)错误的请求"的响应时,我得到一个错误.请参见下面的代码.

I'm using the QBO API and am at the step where I'm trying to retrieve the access and refresh tokens. When I send my request I get an error when I try to get the response that states "The remote server returned an error: (400) Bad Request". See the code below.

我在标头中尝试了多种变体,但无法使其正常工作.

I've tried a number of variations in my header, but cannot get it to work.

有什么想法吗?

代码(使用vb.net):

Code (using vb.net):

Sub Step2_GetTokens()
    'Delcare variables. 
    Dim vHTTPREQUEST As HttpWebRequest
    Dim vHTTPRESPONSE As HttpWebResponse
    Dim vSTREAMOBJECT As Stream
    Dim vSTREAMREADER As StreamReader
    Dim vSTREAMDATA As String

    Dim vAUTHORIZATIONCODE As String
    Dim vREDIRECTURI As String
    Dim vCLIENTID As String
    Dim vCLIENTSECRET As String
    Dim vURI As String
    Dim vTOKEN As String

    'Set variables. 
    vAUTHORIZATIONCODE = "myauthorizationcodefrompreviousstep"
    vREDIRECTURI = "http://localhost:8000/myredirectpage.aspx"
    vCLIENTID = "myclientid"
    vCLIENTSECRET = "myclientsecret"

    'Set URI and Token. 
    vURI = String.Format("https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer?grant_type=authorization_code&code={0}&redirect_uri={1}", vAUTHORIZATIONCODE, vREDIRECTURI)
    vTOKEN = "Basic " + System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(vCLIENTID + ":" + vCLIENTSECRET))

    'Create and send Http request.
    vHTTPREQUEST = CType(WebRequest.Create(vURI), HttpWebRequest)
    vHTTPREQUEST.Method = "POST"
    vHTTPREQUEST.ContentLength = 0
    vHTTPREQUEST.Credentials = CredentialCache.DefaultCredentials
    vHTTPREQUEST.ContentType = "application/x-www-form-urlencoded"
    vHTTPREQUEST.Accept = "application/json"
    vHTTPREQUEST.Headers.Add("Authorization", vTOKEN)

    'Return Http response.   THE ERROR OCCURS AT NEXT LINE
    vHTTPRESPONSE = CType(vHTTPREQUEST.GetResponse(), HttpWebResponse)
    vSTREAMOBJECT = vHTTPRESPONSE.GetResponseStream()
    vSTREAMREADER = New StreamReader(vSTREAMOBJECT, Text.Encoding.UTF8)
    vSTREAMDATA = vSTREAMREADER.ReadToEnd()

    'Display results from respense.
    TextBox1.Text = vSTREAMDATA

    'All done. 
    vHTTPRESPONSE.Close()
    vSTREAMREADER.Close()
End Sub

推荐答案

万岁!我从他们的论坛上的Intuit开发人员那里得到了我问题的答案,但也想在这里分享,以防其他人正在寻找.问题是我需要将URI中的参数放在HTTP请求正文中,而不是URI本身中.

Hooray! I got the answer to my question from an Intuit Developer on their forum, but wanted to share here as well in case others are looking. The issue was I needed to place the parameters that were in my URI in the HTTP request body instead of in the URI itself.

更具体地说,我原始代码段的以下部分设置了传递给请求的URI的值.如果仔细看,它具有参数"grant_type","code"和"redirect_uri".需要从此处删除这些内容,并将其添加到HTTP POST请求的正文中.

To be more specific, the following part of my original code snippet sets the value for the URI being passed to the request. If you look closely it has the parameters "grant_type", "code", and "redirect_uri". Those need to be removed from here and added to the body of the HTTP POST request instead.

vURI = String.Format("https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer?grant_type=authorization_code&code={0}&redirect_uri={1}", vAUTHORIZATIONCODE, vREDIRECTURI)

不知道为什么,或者为什么要这样设置,但这就是它的实质……以后需要研究.现在,这是下面完整的修改后的代码,可以正常工作.

Not sure why, or why they set it up that way but it is what it is... something to look into later. For now, here is the full revised code below that works perfectly.

在到达那里之前,请注意其他几处更改,这些更改实际上并没有真正的影响,但是您应该知道.

Before you get there, note a couple other changes made that didn't really have any impact, but you should likely be aware of.

  1. 我删除了"vHTTPREQUEST.Credentials = CredentialCache.DefaultCredentials"行,因为@ evry1falls指出的没有必要

  1. I removed the line "vHTTPREQUEST.Credentials = CredentialCache.DefaultCredentials" because it was not necessary as pointed out by @evry1falls

我根据字节长度移动并更改了"vHTTPREQUEST.ContentLength = 0"行(请参见添加的新部分).与开发人员建议的更改相匹配,并且可能比我最初做的更好.

I moved and changed the line "vHTTPREQUEST.ContentLength = 0" to be based on the byte length (see new section added). This matched the suggested change from the developer and likely better practice than what I did originally.

这是我从Intuit开发人员那里获得的解决方案所基于的示例代码.在"c"中,因此我将其转换为"vb".

Here is the sample code that I based my solution on that I got from the Intuit developer. It's in "c", so I converted it to "vb".

https://github.com/IntuitDeveloper/OAuth2-Dotnet-WithoutSDK/blob/08d47724c5879d30da9cd8d6b365751514462953/OAuth2_SampleApp_Dotnet/OAuth2Manager.aspx.cs#L423

这是新代码(使用vb.net)

Here's the new code (using vb.net)

Sub step2_get_token()
    'Delcare variables. 
    Dim vHTTPREQUEST As HttpWebRequest
    Dim vHTTPRESPONSE As HttpWebResponse
    Dim vSTREAMOBJECT As Stream
    Dim vSTREAMREADER As StreamReader
    Dim vSTREAMDATA As String

    Dim vAUTHORIZATIONCODE As String
    Dim vREDIRECTURI As String
    Dim vCLIENTID As String
    Dim vCLIENTSECRET As String
    Dim vURI As String
    Dim vTOKEN As String

    'Set variables. 
    vAUTHORIZATIONCODE = "myauthorizationcodefrompreviousstep"
    vREDIRECTURI = "http://localhost:8000/myredirectpage.aspx"
    vCLIENTID = "myclientid"
    vCLIENTSECRET = "myclientsecret"

    'Set URI and Token. NOTE: I removed the parameters from the URI. 
    vURI = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"
    vTOKEN = "Basic " + System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(vCLIENTID + ":" + vCLIENTSECRET))

    'Create a Http request with the appropriate headers. NOTE: I removed the credentials line and moved the content length line.
    vHTTPREQUEST = CType(WebRequest.Create(vURI), HttpWebRequest)
    vHTTPREQUEST.Method = "POST"
    vHTTPREQUEST.ContentType = "application/x-www-form-urlencoded"
    vHTTPREQUEST.Accept = "application/json"
    vHTTPREQUEST.Headers.Add("Authorization", vTOKEN)

    '*** HERE IS THE SECTION I ADDED ***
    'Build request body. Not sure how this is working, but it works. (You can use UTF8 or ASCII)
    Dim vHTTPREQUESTBODY As String = String.Format("grant_type=authorization_code&code={0}&redirect_uri={1}", vAUTHORIZATIONCODE, System.Uri.EscapeDataString(vREDIRECTURI))
    Dim vBYTEVERSIONOFBODY As Byte() = Encoding.ASCII.GetBytes(vHTTPREQUESTBODY)
    vHTTPREQUEST.ContentLength = vBYTEVERSIONOFBODY.Length
    Dim vDATASTREAM As Stream = vHTTPREQUEST.GetRequestStream()
    vDATASTREAM.Write(vBYTEVERSIONOFBODY, 0, vBYTEVERSIONOFBODY.Length)
    vDATASTREAM.Close()
    '*** END OF THE SECTION I ADDED ***

    'Send Http request and get a repsonse. The "GetResponse" method both sends a request to an Internet resource and returns a WebResponse instance.
    vHTTPRESPONSE = CType(vHTTPREQUEST.GetResponse(), HttpWebResponse)
    vSTREAMOBJECT = vHTTPRESPONSE.GetResponseStream()
    vSTREAMREADER = New StreamReader(vSTREAMOBJECT, Text.Encoding.UTF8)
    vSTREAMDATA = vSTREAMREADER.ReadToEnd()

    'Set output parameter.
    TextBox1.Text = vSTREAMDATA

    'All done. 
    vHTTPRESPONSE.Close()
    vSTREAMREADER.Close()
End Sub

这篇关于QuickBooks Online Api错误“远程服务器返回错误:(400)错误的请求".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆