我想使用 oauth2 在 Go 中实现 Ebay 浏览 API 客户端凭据授权流程包裹。这是我的 conf 结构:
conf := clientcredentials.Config{
ClientID: "My Client ID",
ClientSecret: "My client Secret",
Scopes: []string{"https://api.ebay.com/oauth/api_scope"},
TokenURL: "https://api.sandbox.ebay.com/identity/v1/oauth2/token",
EndpointParams: url.Values{"redirect_uri": {"the RuName"}},
}
然后我尝试通过创建一个 *http.Request 来发出请求:
req, err := http.NewRequest("GET", "https://api.sandbox.ebay.com/buy/browse/v1/item/v1|202117468662|0?fieldgroups=COMPACT", nil)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
client := conf.Client(ctx)
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
但后来我得到:
{
"errors" : [ {
"errorId" : 1003,
"domain" : "OAuth",
"category" : "REQUEST",
"message" : "Token type in the Authorization header is invalid:Application",
"longMessage" : "Token type in the Authorization header is invalid:Application"
} ]
}
事实证明,在向资源服务器发出请求时,Go 的包将 Authorization header 设置为:
Authorization: Application Access Token token_string
因此,我尝试通过以下操作将 token 类型手动设置为 Bearer:
tok, err := conf.Token(ctx)
if err != nil {
log.Fatal(err)
}
client := conf.Client(ctx)
req, err := http.NewRequest("GET", "https://api.sandbox.ebay.com/buy/browse/v1/item/v1|202117468662|0?fieldgroups=COMPACT", nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer " tok.AccessToken)
res, err := client.Do(req)
但我继续收到同样的错误。
最佳答案
因此,Ebay 对 OAuth2.0 规范的实现存在问题(错误?),我不得不重新实现 oauth2/clientcredentials 包的 TokenSource 以将 token 类型设置为 Bearer 而不是应用程序访问 token 。
关于go - ebay API 客户端凭据授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49272402/