PHP调用PayPal GetUserinfo接口仅返回user_id,需获取email和payer_id
Hey there! Let's figure out why you're only getting user_id back from PayPal's Identity API instead of the email and payer_id you need. Here are the key checks and fixes to try:
This is the most common culprit. Even if you've enabled all permissions in your PayPal app, if you didn't request the right scopes when generating the access token, PayPal won't return extra user data.
Make sure your authorization request includes these critical scopes:
openid(required for OpenID Connect flow)email(to retrieve the user's email)profile(to get profile-related data like payer_id)
For example, your authorization URL should look something like this (scopes encoded properly):https://www.sandbox.paypal.com/signin/authorize?client_id=YOUR_CLIENT_ID&response_type=code&scope=openid%20email%20profile&redirect_uri=YOUR_REDIRECT_URI
If you missed these scopes earlier, regenerate your access token with them included.
Ensure you're calling the right sandbox endpoint for user info:https://api-m.sandbox.paypal.com/v1/identity/oauth2/userinfo
Your request must be a GET method, with the access token in the Authorization header. Here's a corrected PHP example to reference:
function getPayPalUserInfo($access_token) { $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://api-m.sandbox.paypal.com/v1/identity/oauth2/userinfo", CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Bearer {$access_token}", "Content-Type: application/json" ], CURLOPT_SSL_VERIFYPEER => true // Always keep this enabled in production! ]); $response = curl_exec($curl); $error = curl_error($curl); curl_close($curl); if ($error) { die("Curl Error: " . $error); } return json_decode($response, true); } // Usage $userData = getPayPalUserInfo($your_access_token); print_r($userData);
To confirm what permissions your token actually has, use PayPal's Token Introspection endpoint. Send a POST request to:https://api-m.sandbox.paypal.com/v1/oauth2/token/introspect
Include these parameters:
token={your_access_token}- Use your sandbox client ID and secret for Basic Auth
The response will show a scope field—verify it includes email and profile. If not, your token was generated with insufficient scopes.
Sometimes sandbox test accounts have incomplete profile data, which prevents the API from returning email or payer_id. Log into the PayPal Developer Dashboard, go to Sandbox > Accounts, edit your test account, and make sure:
- Email Address is filled in
- Basic profile details (name, etc.) are complete
If you've tried all these steps and still run into issues, check for curl errors in your code (using curl_error()) to debug any hidden request failures.
内容的提问来源于stack exchange,提问作者gaurav malik

