dtihe8614 2013-11-07 18:18
浏览 212
已采纳

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

The specific 401 error message I get is:

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

This error isn't listed anywhere in the PayPal documentation. I am certain I am using the test credentials and using the correct sandbox endpoint. The error occurs when I attempt to get an access token.

This is the class where the access token is retrieved:

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;
}

This is the $connection variable I have when I print_r (I removed the authorization string):

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 ))

As far I can tell, I have correct credentials, correct endpoint, not sure what else it could be?

  • 写回答

2条回答 默认 最新

  • duanlin1933 2013-11-11 14:15
    关注

    A HTTP401 on /token is returned if the client_id/secret aren’t recognized (either the wrong credentials for the environment, or the credentials aren’t active).

    Can you run a test via cURL to rule out any environment / code-specific issues?

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

    (Replace client_id and secret above with your own)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 分析下图所示同步时序逻辑电路的逻辑功能。
  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集