当我想使用PayPal登录时,我总是收到以下错误:
Fatal error: Uncaught exception 'PayPal\Exception\PayPalInvalidCredentialException' with message 'Credential not found for default user. Please make sure your configuration/APIContext has credential information' in /home/.sites/137/site1611/web/Website/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalCredentialManager.php:154
我正确实施了PayPal PHP SDK,并且我已经在PayPal开发人员仪表板中创建了一个Sandbox用户帐户。我还检索了正确的refresh_token
和access_token
,但我无法检索用户信息,如电子邮件,姓名等。我做错了什么?
我在“使用PayPal登录”页面上的JS看起来像这样:
<span id="myContainer" style="position: absolute;top: 0;left: 0;z-index: 1000;"></span>
<script src="https://www.paypalobjects.com/js/external/api.js"></script>
<script>
paypal.use( ["login"], function(login) {
login.render ({
"appid": "ATAoL...nifIi",
"authend": "sandbox",
"scopes": "profile email address https://uri.paypal.com/services/paypalattributes",
"containerid": "myContainer",
"locale": "en-us",
"returnurl": "http://www.url.com/return.php"
});
});
</script>
我在returnurl
的PHP脚本看起来像这样:
error_reporting(E_ALL);
require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
use PayPal\Rest\ApiContext;
use PayPal\Api\OpenIdTokeninfo;
use PayPal\Api\OpenIdUserinfo;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;
$code = $_GET['code'];
$clientId = 'ATAoLjBG....AbL4vWj89y89nifIi';
$clientSecret = 'EKoaU4uh....YXwCjlCj6FadrRXAdx';
$apiContext = new ApiContext(new OAuthTokenCredential($clientId, $clientSecret));
try {
$accessToken = OpenIdTokeninfo::createFromAuthorizationCode(array('code' => $code), null, null, $apiContext);
}
catch (PayPalConnectionException $ex) {
print_r('###################### Error'); exit(1);
}
print_r('###################### Success: ' . $accessToken);
$user = OpenIdUserinfo::getUserinfo(array('access_token' => $accessToken, $apiContext));
print_r($user);
答案 0 :(得分:0)
在你的最后一行,你错误地在数组内传递apicontext!解决这个问题,你应该没问题。
您可以在此处查看示例代码:http://paypal.github.io/PayPal-PHP-SDK/sample/doc/lipp/GetUserInfo.html
应该是这样的:
$user = OpenIdUserinfo::getUserinfo(array('access_token' => $accessToken), $apiContext);
print_r($user);