是否有方法通過不使用PayPal SDK爲第三方生成身份驗證標頭。我已經獲得了他們的訪問令牌和祕密。然而,這些都是通過對我來說很直接的捲曲請求。無論如何要這樣做?生成身份驗證標頭
另外,要生成頭文件,我需要第三方的API簽名?我如何得到這個?
謝謝你的幫助。
是否有方法通過不使用PayPal SDK爲第三方生成身份驗證標頭。我已經獲得了他們的訪問令牌和祕密。然而,這些都是通過對我來說很直接的捲曲請求。無論如何要這樣做?生成身份驗證標頭
另外,要生成頭文件,我需要第三方的API簽名?我如何得到這個?
謝謝你的幫助。
爲別人尋找的node.js解決方案的PayPal X-PAYPAL-Authorization頭值,我寫了這個:
function PayPalURLEncoder(s)
{
var hex = "abcdef";
var untouched = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
var result = s.split('').map(function(c){
if (untouched.indexOf(c)>=0) { return c; }
else if (c==' ') { return "+"; }
else
{
// Needs converting to HEX
var code = c.charCodeAt(0);
return "%" + hex.charAt((code & 0xf0) >> 4) + hex.charAt(code & 0xf);
}
});
return result.join('');
}
exports.authorizationToken = function(accessToken,method,endpoint)
{
var PARAM_DELIMETER = "&";
var PARAM_SEPERATOR = "=";
var token = { key : accessToken.token, secret : accessToken.tokenSecret};
var consumer = { key : global.config.paypal.username, secret : global.config.paypal.password };
// Add params
var params = {
"oauth_consumer_key" : consumer.key,
"oauth_version" : "1.0",
"oauth_signature_method" : "HMAC-SHA1",
"oauth_token" : token.key,
"oauth_timestamp" : Math.round(new Date().getTime()/1000),
};
// Convert params into paramString
var paramKeys = [];
var paramString = "";
for (var p in params) { paramKeys.push(p); } paramKeys.sort();
for (var i=0; i<paramKeys.length; i+=1)
{
var p = paramKeys[i];
paramString += (p + PARAM_SEPERATOR + params[p]);
if (i+1<paramKeys.length) paramString += PARAM_DELIMETER;
}
// Create signature
var key = PayPalURLEncoder(consumer.secret) + PARAM_DELIMETER + PayPalURLEncoder(token.secret);
var signatureBase = method + PARAM_DELIMETER + PayPalURLEncoder(endpoint) + PARAM_DELIMETER + PayPalURLEncoder(paramString);
var signature = CryptoJS.HmacSHA1(signatureBase, key).toString(CryptoJS.enc.Base64);
return "token="+token.key+",signature="+signature+",timestamp="+params["oauth_timestamp"];
};
的authToken從使用調用 「/權限/ GetAccessToken」 返回通常的方法,並且包含代表第三方操作的令牌和令牌祕密對。方法將是POST,並且終點將是類似於「https://svcs.sandbox.paypal.com/Permissions/GetBasicPersonalData」的東西。
使用上面的方法可能是這個樣子:
exports.basicDetails = function(accessToken, callback)
{
var http = require('https');
var host = global.config.paypal.sandbox ? 'svcs.sandbox.paypal.com' : 'svcs.paypal.com';
var path = '/Permissions/GetBasicPersonalData';
var options = {
host: host,
path: path,
method: 'POST',
headers: {
"X-PAYPAL-AUTHORIZATION" : exports.authorizationToken(accessToken,"POST","https://"+host+path),
"X-PAYPAL-REQUEST-DATA-FORMAT" : "NV",
"X-PAYPAL-RESPONSE-DATA-FORMAT" : "JSON",
"X-PAYPAL-APPLICATION-ID" : global.config.paypal.sandbox ? "<<YOURAPPIDSANDBOX>>" : "<<YOURAPPID>>",
},
};
var req = http.request(options, function(res){
var str = "";
res.setEncoding('utf8');
res.on('data', function (chunk) { str += chunk; });
res.on('end', function() {
if (callback) callback(false,JSON.parse(str));
});
});
req.on('error',function(e){
if (callback) callback("Unable to connect with PayPal");
});
req.end("attributeList.attribute(0)=http://axschema.org/contact/email&attributeList.attribute(1)=http://schema.openid.net/contact/fullname&requestEnvelope.errorLanguage=en_US");
};
基於由阿庫共享的代碼,我已經把它移植到了Python。不幸的是,這仍然導致錯誤代碼爲10002的驗證失敗響應。FWIW,這裏是:
from hashlib import sha1
import hmac
from base64 import b64encode
from urllib import urlencode
from datetime import datetime
def paypal_urlencode(s):
encode = lambda x: x if x in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_+" else '%%%x' % ord(x)
return ''.join(map(encode, s.replace(' ','+')))
def paypal_authorisation(token, ep, consumer, method="POST", sandbox=True):
params = dict(
oauth_consumer_key=consumer['key'],
oauth_version='1.0',
oauth_signature_method="HMAC-SHA1",
oauth_token=token['key'],
oauth_timestamp=datetime.now().strftime('%s')
)
key = "&".join((paypal_urlencode(consumer['secret']), paypal_urlencode(token['secret'])))
sig_base = "&".join((method, paypal_urlencode(ep), paypal_urlencode("oauth_consumer_key=%(oauth_consumer_key)s&oauth_signature_method=%(oauth_signature_method)s&oauth_timestamp=%(oauth_timestamp)s&oauth_token=%(oauth_token)s&oauth_version=%(oauth_version)s" % params)))
h= hmac.new(key.encode('ascii'), sig_base.encode('ascii'), sha1)
signature=b64encode(h.digest())
return "token=%s,signature=%s,timestamp=%s" % (token['key'], signature, params['oauth_timestamp'])
您能澄清這是哪種產品? – Robert
當然。我正在使用1c企業版,並且我如何通過將Curl調用分解爲HTTPRequests等進行調用。這裏是我如何調用請求令牌的示例(替換了我的API詳細信息)。在這種情況下,我必須嘗試以相同的方式獲取api簽名或身份驗證標頭。可悲的是,這是我能做的極限。 http://pastebin.com/UWir6t35 – user2631182
對不起,我的意思是貝寶產品。看着粘貼,我可以看到你的意思是權限API。 – Robert