2014-09-21 56 views
1

爲了確認付款,我需要一個每8小時過期的access_token,因此我需要在嘗試驗證付款之前使用雲代碼獲取新的訪問令牌。使用Parse.com雲代碼確認PayPal付款

我試圖使用的HttpRequest:

function requestAccessToken(response){ 
     Parse.Cloud.httpRequest({ 
      method: 'POST', 
      url: 'https://api.paypal.com/v1/oauth2/token', 
      headers: { 
       'Content-Type': 'application/json', 
       'Accept-Language' : 'en_US' 
      }, 
      body: 'grant_type=client_credentials', 
      success: function(httpResponse) { 
       var res = JSON.parse(httpResponse.text); 
       response.success(res.access_token); 
      }, 
      error: function(httpResponse) { 
       response.error('requestAccessToken failed with response code ' + httpResponse.status); 
      } 
     }); 
    } 

要做到以下捲曲(from docs):

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \ 
    -H "Accept: application/json" \ 
    -H "Accept-Language: en_US" \ 
    -u "{client-ID}:{secret}" \ 
    -d "grant_type=client_credentials" 

的問題是,我無法找到一個方法來添加「 {client-ID}:{secret}「給httpRequest。

使用url: 'https://'+clientId+':'+secret+'@api.paypal.com/v1/oauth2/token'會產生500個服務器響應。

任何想法我如何獲得訪問令牌來驗證付款?

+0

你對你的錯誤信息?另外,你是否嘗試過使用curl命令來驗證你在那裏得到了什麼? – 2014-09-22 07:47:19

+0

如果您正在使用節點,您可能需要查看[正式支持的節點sdk](https://github.com/paypal/rest-api-sdk-nodejs)或查看它是如何工作的[授權(https://github.com/paypal/rest-api-sdk-nodejs/blob/master/lib/api.js) – 2014-09-23 16:42:46

回答

0

您需要添加一個授權頭

 Authorization: Basic < {client-ID}:{secret}" encoded base64> (e.g. QWxhZGRpbjpvcGVuIHNlc2FtZQ==)