【问题标题】:How to get OAuth token from ebay API using express, node, javascript如何使用 express、node、javascript 从 ebay API 获取 OAuth 令牌
【发布时间】:2020-02-03 06:49:21
【问题描述】:

从 eBay 获取 Oauth 令牌需要什么样的请求和响应组合?什么是 runname,我需要哪些标头才能让 eBay 满意?

【问题讨论】:

    标签: javascript node.js express oauth ebay-api


    【解决方案1】:

    我们还必须为 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

    一些提示:

    • 使用“范围”,您可以告诉 eBay 您打算使用什么。你可以找到 描述 here,在 Sandbox/Production Keys Box 下。 (OAuth 范围)
    • 如果你使用 axios,你可以使用 auth 配置,所以你不用 需要btoa:
    axios("https://api.sandbox.ebay.com/identity/v1/oauth2/token", {
      // ...
      auth: {
        username: 'appId',
        password: 'certId'
      }
    });
    
    • 使用不带 https 的沙箱,例如localhost,您可以在 https 站点上设置重定向并将代码重定向/传递到非 https 站点。

    【讨论】:

    • 这太疯狂了!感谢您告诉我有关 Axios 身份验证配置的信息。我不知道那是一回事。您的 Lib 绝对简化了一切。我不再需要这样做了,但很高兴知道它就在那里。
    • @Dantio 我也可以添加这个库太棒了!!!非常感谢。
    • @Danito - 使用您创建的 api,我不断收到错误消息,显示错误":"invalid_grant","error_description":"提供的授权刷新令牌无效或已颁发给另一个客户端"。这是针对沙盒环境的。您知道可能导致此问题的原因吗?那是您上面引用的内容。谢谢
    • @ste 没有更多细节很难说。你确定你没有两次发行令牌吗?如果您可以在 GitHub 上创建票证,那就太好了。您也可以尝试使用环境变量:DEBUG=ebay 来查看更多日志。
    【解决方案2】:

    经过三天令人沮丧的尝试让 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));
    });
    

    一些让我着迷的问题。

    • 确保授权中“基本”后有空格 标题。
    • bota 是一个第 3 方库,它使用 base 64 对您的公共和 密钥。有很多方法可以做到这一点。我之所以这样做,是因为我偷了一堆代码。
    • 使用 Axios,请求体称为数据,但带有 fetch 和其他 方法它可能被称为其他东西,如 body 或 param
    • 由于来自 ebay 的重定向,Axios 方法在 get 请求中 默认为 http 获取。
    • ebay 现在使用 https。确保您正在使用 沙盒网址

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多