我在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
頭添加到我的要求?
我該怎麼做上面,同時用雖然要求實施認證您好!OAuth1?我嘗試添加'.Header.Set(「Accept」,「application/json」)',並且它是無效的(由於我使用的包)?? – James
交換了包並使用''工作。 Header.Set()'。感謝您的輸入。 – James
奇怪,它似乎oauth1包使用本機http.Client從去也https://github.com/dghubble/oauth1/blob/master/config.go#L42 –