【问题标题】:Where to put Grant_Type in XHR在 XHR 中放置 Grant_Type 的位置
【发布时间】:2017-08-09 10:15:35
【问题描述】:

我尝试通过 TypeScript 中的 XHR-Request 访问我的 OAuth2 令牌。我不断收到 400: {"error":"unsupported_grant_type","error_description":"Grant Type is NULL"}

我通过在 iOS 模拟器上运行的 NativeScript 应用程序(在 TypeScript 模板中)拨打电话。

我的代码:

    var oReq = new XMLHttpRequest();     
    oReq.open("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", true);
  //  oReq.withCredentials = true;
  //  oReq.setRequestHeader('grant_type', "client_credentials");
  // where client_credentials = clientID:secret 

    oReq.setRequestHeader('Authorization', "Basic " + this.authorizationBasic); 
//where AutorizationBasic is the base64 String of my credentials
    oReq.setRequestHeader('Accept', "application/json");
    oReq.setRequestHeader('Accept-Language', "en_US");

    oReq.onreadystatechange = function () {
        console.log("state changed - new state: " + oReq.readyState + " and Status: " + oReq.status);
        if (oReq.readyState === 4) {
            if (oReq.status === 200) {
                console.log(oReq.responseText);
            } else { 
                console.log("Error: " + oReq.status + " Message: " + oReq.responseText);
            } 
        }    
    };
    console.log("Status: " + oReq.status);  
    oReq.send();

在做了一些研究后,我发现其他一些人在使用 PayPal API 时遇到了同样的问题(例如here)。有人解决了这个问题吗?是不是我的代码错了?在上面的代码中,我评论了我尝试过的其他一些事情,似乎没有任何效果。

API: https://developer.paypal.com/docs/integration/direct/make-your-first-call/

我非常拼命地尝试,因为简单调用该 API 需要很长时间。我用我的 clientID + secret 尝试了 curl 命令,这对我有用。有谁知道该怎么做?有没有比使用 XHR 更简单的方法?

【问题讨论】:

    标签: javascript typescript paypal xmlhttprequest nativescript


    【解决方案1】:

    如果其他人正在搜索这个:

    您需要在 xhr.send()-Method 中提供 Grant Type 作为主体:

    xhr.send('grant_type=client_credentials');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2014-03-10
      • 2023-03-03
      相关资源
      最近更新 更多