2017-08-09 31 views
1

我試圖通過TypeScript中的XHR-Request訪問我的OAuth2令牌。我一直得到400:{「error」:「unsupported_grant_type」,「error_description」:「授予類型爲NULL」}將Grant_Type放在XHR中的位置

我通過在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(); 

之後做一些研究,我發現,有些人曾與貝寶API(例如here)相同的問題。有人解決了這個問題嗎?這可能是我的代碼錯了嗎?在上面的代碼中,我評論了一些我嘗試過的其他東西,似乎沒有任何效果。

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

我想很拼命,因爲這是服用時間太長,以簡單的調用API。我用我的clientID +密碼嘗試了curl-command,這對我很有用。有沒有人有想法該怎麼做?有沒有比使用XHR更簡單的方法?

回答

1

如果有人正在尋找這樣的:

您需要提供交付式的身體xhr.send() - 方法:

xhr.send('grant_type=client_credentials'); 
相關問題