英文:
How to properly add OAuth headers for a QuickBooks API call in Google Go
问题
我只是试图建立一个概念验证来测试与QB API的连接,以连接到QB Online帐户。我以前从未尝试过像这样建立OAuth连接,所以我不确定我是否做对了。以下是我目前的代码,它发送了请求,但我从QB的服务器收到了401错误(未经授权的OAuth令牌:signature_invalid401SERVER):
client := &http.Client{}
if req, err := http.NewRequest("GET", "https://qbo.intuit.com/qbo1/resource/customers/v2/717594130", nil); err != nil {
//处理错误
} else {
req.Header.Add("Authorization", "OAuth oauth_token=\"MY_TOKEN\",oauth_nonce=\"7758caa9-e1f4-4fa1-84c5-5759fd513a88\",oauth_consumer_key=\"MY_KEY\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1369259523\",oauth_version=\"1.0\",oauth_signature=\"MY_SIG\"")
if resp, err := client.Do(req); err != nil {
//处理错误
} else {
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
//处理错误
}
myOutput := string(contents)
}
}
问题可能是我的QB帐户设置有问题吗?有一个名为“主机名域”的设置,我认为它可能只允许从我在那里输入的地方进行连接(目前是intuit.com)。如果是这种情况,我该如何设置以允许从我的本地主机上的开发应用程序进行连接?
英文:
I am just trying to get a proof of concept working to test a connection to the QB api for a QB Online account. I have never tried to make an OAuth connection before like this, so I'm not sure that I am doing it right. Here's what I have so far, and it makes the request but I get a 401 error returned from QB's server (Unauthorized OAuth Token: signature_invalid401SERVER):
client := &http.Client{}
if req, err := http.NewRequest("GET", "https://qbo.intuit.com/qbo1/resource/customers/v2/717594130", nil); err != nil {
//handle error
} else {
req.Header.Add("Authorization", "OAuth oauth_token=\"MY_TOKEN\",oauth_nonce=\"7758caa9-e1f4-4fa1-84c5-5759fd513a88\",oauth_consumer_key=\"MY_KEY\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1369259523\",oauth_version=\"1.0\",oauth_signature=\"MY_SIG\"")
if resp, err := client.Do(req); err != nil {
//handle error
} else {
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
//handle error
}
myOutput := string(contents)
}
}
Could the problem may be with my settings on my QB account instead? There is a setting for "Host Name Domain" that I think it might only allow connections from what I have entered there (which is currently intuit.com). If that is the case, how do I set that to allow connections from my dev app on my localhost?
答案1
得分: 0
你是否使用了正确的OAuth算法来生成签名?
你能否发布一个实际的请求/响应,显示出站签名/OAuth头部,以及从Intuit收到的响应?
你的代码没有显示任何这些内容,而且看起来你没有使用Intuit DevKit,所以那可能是开始的地方。我猜测你发送的签名可能无效。我强烈建议你找一个OAuth库,并使用该OAuth库,而不是尝试自己编写OAuth算法。
至于这个问题:
问题可能是我在我的QB账户上的设置吗?
有一个名为“主机名域”的设置,我认为它可能只允许从我在那里输入的地方进行连接(目前是intuit.com)。
这不太可能是问题...但是要进一步进行简单的测试请求,你需要将其设置为你的主机名。如果是本地开发框,你可以输入你的本地开发框主机名(例如http://localhost/
或http://192.168.1.2/
或任何类似的URL都可以 - 无论你用什么URL访问你的开发框都可以)。
英文:
Are you using the correct OAuth algorithm to generate the signature?
Can you post an actual request/response that shows the outgoing signature/OAuth header, and the response you get back from Intuit?
Your code doesn't show any of that, and it doesn't look like you're using an Intuit DevKit, so that's probably the place to start. My guess would be that the signature you're sending isn't valid. I would highly recommend you find a OAuth library, and use that OAuth library, rather than try to roll your own OAuth algorithm.
As far as this goes:
> Could the problem may be with my settings on my QB account instead?
> There is a setting for "Host Name Domain" that I think it might only
> allow connections from what I have entered there (which is currently
> intuit.com).
That is not likely to be the problem... but to get any further than a simple test request, you will need to set that to your host name. If it's a local dev box, you can enter your local dev boxes hostname (e.g. http://localhost/
or http://192.168.1.2/
or anything like that is fine - whatever URL you use to hit your dev box)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论