Google+:获取离线模式下的友好用户列表


Google+: Get the friendlist of users in offline mode

我想找到Google+用户的朋友列表,他们在没有再次请求授权的情况下授权了该应用程序。

例如:一旦用户使用Google+登录到我的网站,我就会将auth令牌、代码、Google+用户id存储在数据库中。所以一旦完成,我想在他的圈子里找到用户朋友的列表。我设法使第一部分工作起来,它将auth令牌、代码、Google+用户id保存在数据库中。但在第二种情况下,即找到用户朋友列表,我再次看到用户必须授权应用程序的屏幕。

有人能帮我吗?

此外,是否可以在离线模式下获取所有用户详细信息,如使用数据库中的auth令牌、代码、Google+用户id

初始代码(登录):

require_once __DIR__.'/social-api/google/Google_Client.php';
require_once __DIR__.'/social-api/google/contrib/Google_PlusService.php';
$this->sn_obj = new Google_Client($google_app_id, $google_secret_key,  $call_back_url);
$this->plus_obj = new Google_PlusService($this->sn_obj);
$this->sn_obj->setRedirectUri($call_back_url);
$this->sn_obj->setState($redirect_url);
$requestVisibleActions = array('http://schemas.google.com/AddActivity','http://schemas.google.com/ReviewActivity');
$this->sn_obj->setRequestVisibleActions($requestVisibleActions);
$this->sn_obj->setAccessType('offline');
$this->sn_obj->setScopes(array('https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/plus.login'));
$this->sn_obj->createAuthUrl();

在回调页面中:

require_once __DIR__.'/social-api/google/Google_Client.php';
require_once __DIR__.'/social-api/google/contrib/Google_PlusService.php';
$this->sn_obj = new Google_Client($google_app_id, $google_secret_key,  $call_back_url);
$this->plus_obj = new Google_PlusService($this->sn_obj);
$this->sn_obj->setRedirectUri($call_back_url);
$this->sn_obj->setState($redirect_url);
$requestVisibleActions = array('http://schemas.google.com/AddActivity','http://schemas.google.com/ReviewActivity');
$this->sn_obj->setRequestVisibleActions($requestVisibleActions);
$this->sn_obj->setAccessType('offline');
$this->sn_obj->setScopes(array('https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/plus.login'));
$this->sn_obj->authenticate();
$google_auth_token = $this->sn_obj->getAccessToken();
$google_user_info =  $this->plus_obj->people->get('me');

好友列表页面:

require_once __DIR__.'/social-api/google/Google_Client.php';
require_once __DIR__.'/social-api/google/contrib/Google_PlusService.php';
$this->sn_obj = new Google_Client($google_app_id, $google_secret_key,  $call_back_url);
$this->plus_obj = new Google_PlusService($this->sn_obj);
$this->sn_obj->setRedirectUri($call_back_uri);
$this->sn_obj->authenticate();
$user_info = $this->plus_obj->people->listPeople($token['oauth_uid'],'visible');

通常,在脱机模式下工作时,需要存储从服务器返回的整个对象,而不仅仅是auth令牌。此对象包括长期刷新令牌以及有关何时需要进行刷新的信息。将整个令牌传递给服务允许它为您刷新令牌。

你得到的答案看起来像是你手动刷新自己——这很好,但一般来说应该没有必要。

在某些情况下,如果用户在您请求脱机模式之前授权了您的应用程序,而您稍后请求脱机模式,则您仍然无法获得刷新令牌。当被问及是否没有当前或过期的身份验证令牌时,谷歌只会给出刷新令牌。有两种方法:

  1. 当您切换到使用脱机模式时,请使您的旧客户端id/secret无效,然后开始使用新客户端
  2. 让用户通过取消您的应用程序的授权https://accounts.google.com/b/0/IssuedAuthSubTokens或者(如果您使用Google+登录)通过https://plus.google.com/apps

最终成功完成

在好友列表页面:

require_once __DIR__.'/social-api/google/Google_Client.php';
require_once __DIR__.'/social-api/google/contrib/Google_PlusService.php';
$this->sn_obj = new Google_Client($google_app_id, $google_secret_key,  $call_back_url);
$this->plus_obj = new Google_PlusService($this->sn_obj);
if(!$this->sn_obj->getAccessToken()){
    $this->sn_obj->refreshToken($refresh_token_from_db);
}
$google_auth_token = $this->sn_obj->getAccessToken();
$google_auth_token_arr = json_decode($google_auth_token,true);
$url = 'https://www.googleapis.com/plus/v1/people/'.$uid_from_db.'/people/visible?access_token='.$google_auth_token_arr['access_token'].'&format=json';
$c = curl_init($url);
curl_setopt($c, CURLINFO_HEADER_OUT, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
$contents = curl_exec($c);
curl_close($c);
$user_info = json_decode($contents, true);