我有以下来自PayPal Payout SDK的代码,用于从PayPal应用编程接口获取访问令牌。
curl -v POST https://api-m.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "CLIENT_ID:SECRET" \
-d "grant_type=client_credentials"
为了获得访问令牌,我尝试了以下方法。
$client_id = "AWN5555";
$secret = "44444";
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
$data = ['grant_type:client_credentials'];
$response = Http::withHeaders([
'Accept:application/json',
'Accept-Language:en_US',
"Content-Type: application/x-www-form-urlencoded"
])->withBasicAuth($client_id, $secret)
->post($url, $data);
// OR
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization ' => ' Basic ' .
base64_encode($client_id . ':' . $secret)
],
'form_params' => [
'grant_type' => 'client_credentials',
]
]);
发布于 2020-12-26 01:23:37
laravel 7或8解决方案:
$client_id = "AWN5555";
$secret = "44444";
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
$data = [
'grant_type' => 'client_credentials',
];
$response = Http::asForm()
->withBasicAuth($client_id, $secret)
->post($url, $data);
php原生解决方案:
$client_id = "AWN5555";
$secret = "44444";
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api-m.sandbox.paypal.com/v1/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
CURLOPT_HTTPHEADER => [
'Authorization: Basic '.base64_encode($client_id.':'.$secret)
],
]);
$response = curl_exec($curl);
curl_close($curl);
https://stackoverflow.com/questions/65447164
复制相似问题