2014-12-04 2 views
0

Google App Engine은 cURL을 사용할 수 없으므로 사용할 수 없습니다. 내 전화를 구조화되는 방법을 잘 모르겠어요 https://developer.paypal.com/docs/integration/direct/make-your-first-callPHP에서 cURL없이 PayPal API 호출하기

:

나는의 2 단계에서 액세스 토큰을 얻기 위해 노력하고 있어요. 지금까지이 있습니다

$data = array(
    'grant_type' => 'client_credentials', 
    'client_id' => $sandbox_client_id, 
    'secret' => $sandbox_secret 

); 


$data = json_encode($data); 

$context = [ 
    'http' => [ 
    'method' => 'POST', 
    'header' => 'Content-Type: application/x-www-form-urlencoded' . "\r\n" . 
     'Accept: application/json' . "\r\n" . 
     'Accept-Language: en_US' . "\r\n" 
    , 
    'content' => $data 
    ] 
]; 
$context = stream_context_create($context); 

$response = file_get_contents('https://api.sandbox.paypal.com/v1/oauth2/token', false, $context); 

echo $response; 

이 반환

Warning: file_get_contents(https://api.sandbox.paypal.com/v1/oauth2/token): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\folder\php\paypal\test.php on line 28 

어떤 통찰력을 감사합니다!

답변

0

알아 냈습니다.

client idsecret:, base64 encoded으로 구분 된 문자열에 넣어야합니다. 그런 다음 header으로 전송됩니다.

이 작동 :

$auth_string = $sandbox_client_id . ':' . $sandbox_secret; 
$encoded_auth = base64_encode($auth_string); 

$data = array(
    'grant_type' => 'client_credentials' 


); 

$http_query = http_build_query($data); 
$context = [ 
    'http' => [ 
    'method' => 'POST', 
    'header' => 'Content-Type: application/x-www-form-urlencoded' . "\r\n" . 
     'Accept: application/json' . "\r\n" . 
     'Accept-Language: en_US' . "\r\n" . 
     'Authorization: Basic ' . $encoded_auth 
    , 
    'content' => $http_query 
    ] 
]; 


$context = stream_context_create($context); 

$response = file_get_contents('https://api.sandbox.paypal.com/v1/oauth2/token', false, $context); 

echo $response;