【问题标题】:How to retrieve PayPal REST Api access-token using node如何使用节点检索 PayPal REST Api 访问令牌
【发布时间】:2015-04-06 07:49:13
【问题描述】:

如何通过节点获取利用 REST Api 所需的 PayPal 访问令牌?

【问题讨论】:

    标签: node.js paypal npm-request


    【解决方案1】:

    拥有 PayPal 客户端 ID 和客户端密码后,您可以使用以下内容:

    var request = require('request');
    
    request.post({
        uri: "https://api.sandbox.paypal.com/v1/oauth2/token",
        headers: {
            "Accept": "application/json",
            "Accept-Language": "en_US",
            "content-type": "application/x-www-form-urlencoded"
        },
        auth: {
        'user': '---your cliend ID---',
        'pass': '---your client secret---',
        // 'sendImmediately': false
      },
      form: {
        "grant_type": "client_credentials"
      }
    }, function(error, response, body) {
        console.log(body);
    });
    

    如果成功,响应将如下所示:

    {
        "scope":"https://api.paypal.com/v1/payments/.* ---and more URL callable with the access-token---",
        "access_token":"---your access-token---",
        "token_type":"Bearer",
        "app_id":"APP-1234567890",
        "expires_in":28800
    }
    

    【讨论】:

    • 非常感谢您的回复!我怎样才能得到“access_token”,我试过这样的“body.access_token”,但它返回未定义。
    • @Dimitri const { access_token } = JSON.parse(body); 我们会把它翻译成原生 fetch() 吗?
    【解决方案2】:

    另外,您可以使用axiosasync/await

    const axios = require('axios');
    
    (async () => {
      try {
        const { data: { access_token } } = await axios({
          url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
          method: 'post',
          headers: {
            Accept: 'application/json',
            'Accept-Language': 'en_US',
            'content-type': 'application/x-www-form-urlencoded',
          },
          auth: {
            username: client_id,
            password: client_secret,
          },
          params: {
            grant_type: 'client_credentials',
          },
        });
    
        console.log('access_token: ', access_token);
      } catch (e) {
        console.error(e);
      }
    })();
    

    【讨论】:

    • 感谢您的解决方案。我从 ajax 转移到 axios 以生成令牌。效果很好。
    • @Gediminas 太好了,我很高兴能帮上忙。你也可以 +1 这个答案;)
    • 非常感谢,这个解决方案对我有用,因为我使用的是使用 Axios 的 NestJS。
    • @HartWoom 完美。 Heureux de vous 助手。
    【解决方案3】:

    现代问题需要现代解决方案:

    const fetch = require('node-fetch');
    const authUrl = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
    const clientIdAndSecret = "CLIENT_ID:SECRET_CODE";
    const base64 = Buffer.from(clientIdAndSecret).toString('base64')
    
    fetch(authUrl, { 
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Accept-Language': 'en_US',
            'Authorization': `Basic ${base64}`,
        },
        body: 'grant_type=client_credentials'
    }).then(function(response) {
        return response.json();
    }).then(function(data) {
        console.log(data.access_token);
    }).catch(function() {
        console.log("couldn't get auth token");
    });
    

    【讨论】:

    • 你是一个救生员。几天来,我一直在为 PayPal/React 和沙盒订阅测试而苦苦挣扎……我可以让它在产品中工作,但不能在沙盒中工作,这是让它工作的第一步
    【解决方案4】:

    您可以使用 PayPal-Node-SDK 调用 PayPal Rest API。它为您处理所有授权和身份验证。

    【讨论】:

    • 你所说的对于大多数需求都是正确的,但对于“支付体验/网络配置文件”来说却不是这样。
    • 再说一次,不是 nodejs 工程师,但是,我看到 PayPal-Node-SDK 也有 github.com/paypal/PayPal-node-SDK/tree/master/samples/… 的示例
    • 如果您在使用 node-sdk 时仍然发现问题,请告诉我。我们很乐意帮助修复/更新问题,让您更快地集成 paypal api。
    【解决方案5】:

    这是我使用 superagent 获取 access_token 的方法

            superagent.post('https://api.sandbox.paypal.com/v1/oauth2/token')
            .set("Accept","application/json")
            .set("Accept-Language","en_US")
            .set("content-type","application/x-www-form-urlencoded")
            .auth("Your Client Id","Your Secret")
            .send({"grant_type": "client_credentials"})
            .then((res) => console.log("response",res.body))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2023-02-02
      • 2015-04-06
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多