How to get the friend list using Google+ API OAuth

2019-04-02 03:31发布

问题:

I am having signin with Google+ to retrieve the users basic profile info(Name, Urls, Location, Profile Picture). But I want to also retrieve the user-id of the users friend so that i can give him/her better suggestion of whom the user already know in my website.

$app_access_token = GetCH();
function GetCH(){
$ch = curl_init();
$pieces = explode(",", $_SESSION['access_token']); $piece = explode(":", $pieces[0]);
$token = str_replace('"',"",$piece[1]);
$url = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=".$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETU`enter code here`RNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
if(substr($url,0,8)=='https://'){
    curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};

if ($client->getAccessToken()){ 
  $me = $plus->people->get('me');
  $user_id = filter_var($me['id'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
  $url = filter_var($me['url'], FILTER_VALIDATE_URL);
  $img = filter_var($me['image']['url'], FILTER_VALIDATE_URL);
  $name = filter_var($me['displayName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);

  // The access token may have been updated lazily.
  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();

}

For More Information: https://developers.google.com/apis-explorer/#p/plus/v1/ But I am unable to figure how to get the friend list as an array from their API

Thanks in Advance

回答1:

You need to use: $plus->people->listPeople, you can find an example of usage in the PHP quick start app.



回答2:

First of all, you should make sure you're using the https://www.googleapis.com/auth/plus.login OAuth2 scope to make sure they permit you to access some or all of their friends lists. You want to use the people.list API access point as documented here and detailed further on this page. Although they don't give a PHP code sample, I think you'll be able to use the $plus->people->list to make the call and get the information you want.