2017-08-11 55 views
0

我在Go中構建了一個查詢Magento API的服務。在Go中查詢Magento API

我已經擁有了製作請求所需的oauth證書(這些是持久性的),並且能夠成功地在Postman中查詢API。

我試圖採用這種封裝形式查詢Magento的API,但是每次我提出請求我得到一個錯誤:

Service temporary unavailable

找遍四周,看起來這是一個常見的錯誤,以得到當請求沒有Accept: application/json的標題時。

我使用this package來當前簽署我的請求,無法看到添加此標頭的任何方式。如果需要,我願意使用不同的包,它只需要支持oauth1認證。

Go相對來說比較新,我不太確定如何將標題添加到我的請求中,並且會喜歡一些幫助。

這是我當前的代碼:

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頭添加到我的要求?

回答

1

創建一個請求:

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> 
     ... 
+0

我該怎麼做上面,同時用雖然要求實施認證您好!OAuth1?我嘗試添加'.Header.Set(「Accept」,「application/json」)',並且它是無效的(由於我使用的包)?? – James

+0

交換了包並使用''工作。 Header.Set()'。感謝您的輸入。 – James

+0

奇怪,它似乎oauth1包使用本機http.Client從去也https://github.com/dghubble/oauth1/blob/master/config.go#L42 –

0

我終於有花了無數它小時後這方面的工作 - 我換了OAuth的包從dghubble/oauth1nhjk/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) 
}