代码之家  ›  专栏  ›  技术社区  ›  James

在Go中查询Magento API

  •  0
  • James  · 技术社区  · 9 年前

    我正在Go中构建一个查询Magento API的服务。

    我已经拥有发出请求所需的oauth凭据(这些凭据是持久的),并且能够在Postman中成功地查询API。

    服务暂时不可用

    我四处搜索过,当请求没有标题时,这似乎是一个常见错误 Accept: application/json

    我正在使用 this package

    这是我当前的代码:

    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))
    }
    

    如何添加 接受:应用程序/json 我的请求的标题?

    2 回复  |  直到 9 年前
        1
  •  2
  •   Alex Efimov    9 年前

    创建请求:

    req, err := http.NewRequest("GET", path, nil)
    if err != nil {
         // handle error
    }
    

    设置标题:

    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>
          ...
    
        2
  •  0
  •   James    9 年前

    dghubble/oauth1 nhjk/oauth

    这使用本机Go http 所以我们可以像普通一样设置标题,正如@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)
    }