【发布时间】:2020-02-03 06:49:21
【问题描述】:
从 eBay 获取 Oauth 令牌需要什么样的请求和响应组合?什么是 runname,我需要哪些标头才能让 eBay 满意?
【问题讨论】:
标签: javascript node.js express oauth ebay-api
从 eBay 获取 Oauth 令牌需要什么样的请求和响应组合?什么是 runname,我需要哪些标头才能让 eBay 满意?
【问题讨论】:
标签: javascript node.js express oauth ebay-api
我们还必须为 eBay API 使用 JS,并通过开发新的 Lib 解决了您提到的问题。它可用here。如果令牌过期,这个库也会自动尝试刷新。
这就是我们获取 oAuth 令牌的方式:
import eBayApi from '@hendt/ebay-api';
const ebay = new eBayApi({
appId: '-- or Client ID --',
certId: '-- or Client Secret',
sandbox: false,
siteId: eBayApi.SiteId.EBAY_US,
ruName: '-- eBay Redirect URL name --' //in this case: Dean_Schmid-DeanSchm-TestAp-kqmgc
});
// This will generate the URL you need to visit
const url = ebay.oAuth2.generateAuthUrl();
// After grant access, eBay will redirect you to RuName page and set the ?code query.
// Grab the ?code and get the token with:
ebay.oAuth2.getToken(code).then((token) => {
console.log('Token', token);
ebay.oAuth2.setCredentials(token);
// Now you can make request to eBay API:
ebay.buy.browse.getItem('v1|382282567190|651094235351')
.then(item => {
console.log(JSON.stringify(item, null, 2));
})
.catch(e => {
console.log(e);
});
});
我们可以找到另一个范围示例here。
一些提示:
axios("https://api.sandbox.ebay.com/identity/v1/oauth2/token", {
// ...
auth: {
username: 'appId',
password: 'certId'
}
});
【讨论】:
经过三天令人沮丧的尝试让 Ebay 的 oauth 给我一个访问令牌后,我终于解决了。由于文档很痛苦并且在线几乎没有帮助,我决定在这里发布我的解决方案,希望它可以帮助其他人。我不擅长 StackOverflow,所以如果我需要改进我的格式,请告诉我。
app.get("/login/ebay", (req, res) => {
res.redirect(`https://auth.sandbox.ebay.com/oauth2/authorize?client_id=DeanSchm-TestApp-SBX-b843acc90-fd663cbb&redirect_uri=Dean_Schmid-DeanSchm-TestAp-kqmgc&response_type=code`
);
});
您需要做的第一件事是重定向到此 URL。
格式是这样的
https://auth.sandbox.ebay.com/oauth2/authorize?client_id=&redirect_uri=&response_type=code
还有一个作用域属性,不过我还不明白,我拿回了一个token,没有is so me。
该 URL 会将您带到 eBay 登录页面。如果您使用沙盒,则需要创建沙盒用户并使用沙盒凭据登录。
登录后,eBay 会将您重定向到您选择的 URL。您在此处输入要重定向到的 URL。
它位于通过您的应用程序从 Ebay 获取令牌下的 ebay 开发人员部分。
这个 URL 可以是任何东西。您只需要在 node 或 express 或其他任何方式中处理它,因为一旦有人登录该 URL,他们就会前往。
我是这样处理的
app.get("/auth/ebay/callback", (req, res) => {
axios("https://api.sandbox.ebay.com/identity/v1/oauth2/token", {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization:
"Basic " +
btoa(
`client public key:client secret keys`
)
},
data: qs.stringify({
grant_type: "authorization_code",
// parsed from redirect URI after returning from eBay,
code: req.query.code,
// this is set in your dev account, also called RuName
redirect_uri: "Dean_Schmid-DeanSchm-TestAp-kqmgc"
})
})
.then(response => console.log(response))
.catch(err => console.log(err));
});
一些让我着迷的问题。
【讨论】: