PayPal API:例外:401访问https://api.sandbox.paypal.com/v1/oauth2/token

时间:2013-11-07 18:18:18

标签: php paypal oauth-2.0

我得到的具体401错误消息是:

"error":"invalid_client","error_description":"The client credentials are invalid"}"

此错误未在PayPal文档中的任何位置列出。我确信我正在使用测试凭据并使用正确的沙箱端点。当我尝试获取访问令牌时发生错误。

这是检索访问令牌的类:

private function _generateAccessToken($config) {

$base64ClientID = base64_encode($this->clientId . ":" . $this->clientSecret);                           
$headers = array(
    "User-Agent" => PPUserAgent::getValue(RestHandler::$sdkName, RestHandler::$sdkVersion), 
    "Authorization" => "Basic " . $base64ClientID,
    "Accept" => "*/*"
);      
$httpConfiguration = $this->getOAuthHttpConfiguration($config); 
$httpConfiguration->setHeaders($headers); 

$connection = PPConnectionManager::getInstance()->getConnection($httpConfiguration, $config);
//print_r($connection); die;
$res = $connection->execute("grant_type=client_credentials");       

$jsonResponse = json_decode($res, true);
if($jsonResponse == NULL || 
        !isset($jsonResponse["access_token"]) || !isset($jsonResponse["expires_in"]) ) {
    $this->accessToken = NULL;
    $this->tokenExpiresIn = NULL;   
    $this->logger->warning("Could not generate new Access token. Invalid response from server: " . $jsonResponse);      
} else {
    $this->accessToken = $jsonResponse["access_token"];
    $this->tokenExpiresIn = $jsonResponse["expires_in"];
}
$this->tokenCreateTime = time();
return $this->accessToken;
}

这是$connection变量,当我print_r时(我删除了授权字符串):

PayPal\Core\PPHttpConnection Object( [httpConfig:PayPal\Core\PPHttpConnection:private] => PayPal\Core\PPHttpConfig Object ( [headers:PayPal\Core\PPHttpConfig:private] => Array ( [User-    Agent] => PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.4.21;bit=64;os=Linux_2.6.18-    308.16.1.el5;machine=x86_64;openssl=0.9.8e-fips-rhel5;curl=7.24.0) [Authorization] => Basic     REMOVED AUTHORIZATION STRING == [Accept] => */* ) [curlOptions:PayPal\Core\PPHttpConfig:private]     => Array ( [32] => 3 [78] => 30 [19913] => 1 [13] => 60 [10018] => PayPal-PHP-SDK [10023] => Array ( ) [81] => 2 [64] => 1 ) [url:PayPal\Core\PPHttpConfig:private] => https://api.sandbox.paypal.com/v1/oauth2/token [method:PayPal\Core\PPHttpConfig:private] => POST [retryCount:PayPal\Core\PPHttpConfig:private] => 1 ) [logger:PayPal\Core\PPHttpConnection:private] => PayPal\Core\PPLoggingManager Object ( [loggerName:PayPal\Core\PPLoggingManager:private] => PayPal\Core\PPHttpConnection [isLoggingEnabled:PayPal\Core\PPLoggingManager:private] => 1 [loggingLevel:PayPal\Core\PPLoggingManager:private] => 3 [loggerFile:PayPal\Core\PPLoggingManager:private] => PayPal.log ))

据我所知,我有正确的凭据,正确的终点,不确定它可能是什么?

2 个答案:

答案 0 :(得分:3)

如果无法识别client_id / secret(环境的凭据错误或凭据未激活),则会返回/ token上的HTTP401。

您可以通过cURL运行测试以排除任何环境/代码特定的问题吗?

curl -v -u "client_id:secret" "https://api.sandbox.paypal.com/v1/oauth2/token" -X POST -d "response_type=token&grant_type=client_credentials"

(将上面的client_id和秘密替换为您自己的

答案 1 :(得分:0)

确保付款$ config模式为Sandbox,clientId和clientSecret密钥同时为Sandbox。