在 Go 中查询 Magento API
Querying Magento API in Go
我正在用 Go 构建一个查询 Magento 的服务 API。
我已经拥有发出请求所需的 oauth 凭据(这些凭据是永久性的)并且能够在 Postman 中成功查询 API。
我正在尝试使用此包查询 Magento API,但是每次发出请求时都会收到错误消息:
Service temporary unavailable
我四处搜索了一下,当请求没有 Accept: application/json
.
的 header 时,看起来这是一个常见的错误
我目前正在使用 this package 来签署我的请求,但看不到任何添加此 header 的方式。如果需要,我愿意使用不同的包,它只需要支持 oauth1 身份验证。
作为 Go 的新手,我不太确定如何将 header 添加到我的请求中,希望得到一些帮助。
这是我当前的代码:
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/dghubble/oauth1"
)
func main() {
config := oauth1.NewConfig("consumer key", "consumer secret")
token := oauth1.NewToken("token key", "token secret")
httpClient := config.Client(oauth1.NoContext, token)
path := "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC"
resp, err := httpClient.Get(path)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Raw Resonse Body:\n%v\n", string(body))
}
如何将Accept: application/json
header添加到我的请求中?
创建请求:
req, err := http.NewRequest("GET", path, nil)
if err != nil {
// handle error
}
设置headers:
req.Header.Add("Accept", "application/json")
运行 使用问题中配置的客户端的请求:
resp, err := httpClient.Do(req)
if err != nil {
// handle error
}
对我有用的例子:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/dghubble/oauth1"
)
func main() {
config := oauth1.NewConfig("consumer key", "consumer secret")
token := oauth1.NewToken("token key", "token secret")
httpClient := config.Client(oauth1.NoContext, token)
path := "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC"
req, err := http.NewRequest("GET", path, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Accept", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Raw Resonse Body:\n%v\n", string(body))
}
输出:
Raw Resonse Body:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
...
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
...
在花费了无数个小时之后,我终于可以使用它了——我从 dghubble/oauth1 to nhjk/oauth 交换了 oauth 包。
这使用本机 Go http
包,因此我们可以像平常一样设置 headers,正如@AlexEfimov 所概述的那样。
工作代码如下:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/nhjk/oauth"
)
func main() {
ck := "consumer key"
cs := "consumer secret"
tk := "token key"
ts := "token secret"
// create an http client and a request for it to send
client := new(http.Client)
req, _ := http.NewRequest("GET", "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC", nil)
req.Header.Set("Accept", "application/json")
// a consumer allows you to authorize requests with a token
cons := oauth.Consumer{ck, cs}
// authorize request
cons.Authorize(req, &oauth.Token{tk, ts})
// send request and print body
res, _ := client.Do(req)
body, _ := ioutil.ReadAll(res.Body)
fmt.Printf("%s\n", body)
}
我正在用 Go 构建一个查询 Magento 的服务 API。
我已经拥有发出请求所需的 oauth 凭据(这些凭据是永久性的)并且能够在 Postman 中成功查询 API。
我正在尝试使用此包查询 Magento API,但是每次发出请求时都会收到错误消息:
Service temporary unavailable
我四处搜索了一下,当请求没有 Accept: application/json
.
我目前正在使用 this package 来签署我的请求,但看不到任何添加此 header 的方式。如果需要,我愿意使用不同的包,它只需要支持 oauth1 身份验证。
作为 Go 的新手,我不太确定如何将 header 添加到我的请求中,希望得到一些帮助。
这是我当前的代码:
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/dghubble/oauth1"
)
func main() {
config := oauth1.NewConfig("consumer key", "consumer secret")
token := oauth1.NewToken("token key", "token secret")
httpClient := config.Client(oauth1.NoContext, token)
path := "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC"
resp, err := httpClient.Get(path)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Raw Resonse Body:\n%v\n", string(body))
}
如何将Accept: application/json
header添加到我的请求中?
创建请求:
req, err := http.NewRequest("GET", path, nil)
if err != nil {
// handle error
}
设置headers:
req.Header.Add("Accept", "application/json")
运行 使用问题中配置的客户端的请求:
resp, err := httpClient.Do(req)
if err != nil {
// handle error
}
对我有用的例子:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/dghubble/oauth1"
)
func main() {
config := oauth1.NewConfig("consumer key", "consumer secret")
token := oauth1.NewToken("token key", "token secret")
httpClient := config.Client(oauth1.NoContext, token)
path := "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC"
req, err := http.NewRequest("GET", path, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Accept", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Raw Resonse Body:\n%v\n", string(body))
}
输出:
Raw Resonse Body:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
...
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
...
在花费了无数个小时之后,我终于可以使用它了——我从 dghubble/oauth1 to nhjk/oauth 交换了 oauth 包。
这使用本机 Go http
包,因此我们可以像平常一样设置 headers,正如@AlexEfimov 所概述的那样。
工作代码如下:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/nhjk/oauth"
)
func main() {
ck := "consumer key"
cs := "consumer secret"
tk := "token key"
ts := "token secret"
// create an http client and a request for it to send
client := new(http.Client)
req, _ := http.NewRequest("GET", "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC", nil)
req.Header.Set("Accept", "application/json")
// a consumer allows you to authorize requests with a token
cons := oauth.Consumer{ck, cs}
// authorize request
cons.Authorize(req, &oauth.Token{tk, ts})
// send request and print body
res, _ := client.Do(req)
body, _ := ioutil.ReadAll(res.Body)
fmt.Printf("%s\n", body)
}