go - ebay API 客户端凭据授权

标签 go oauth-2.0 ebay-api

我想使用 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/

相关文章:

mongodb - 由于超时,UpdateOne 在客户端失败,但 MongoDB 仍然处理它

concurrency - 为什么 go.net/websocket'api 不使用 channel 来发送和接收消息?

c# - 在.net/c#中调用Marketo Rest Api的示例代码

cakephp - 如何使用 cakedc/users 在 cakephp 中实现 google oauth?

sqlite - 嵌入式键值数据库与每个键只存储一个文件?

function - golang 调用中的参数太多

authentication - OAuth2 : Resource Owner OpenID Grant?

python - eBay SOAP API - 缺少 SOA 操作名称 header

python - 通过 Python 访问 eBay 开发人员的 API?

c# - 易趣查找 API 和 C#