【问题标题】:Google+: Get the friendlist of users in offline modeGoogle+:在离线模式下获取用户的好友列表
【发布时间】:2014-08-26 17:41:03
【问题描述】:

我想查找已授权该应用的 Google+ 用户的好友列表,而无需再次请求授权。

例如:一旦用户使用 Google+ 登录到我的网站,我将身份验证令牌、代码、Google+ 用户 ID 存储在数据库中。 因此,一旦完成,我想在他的圈子中找到用户朋友的列表。 我设法让第一部分工作,它将身份验证令牌、代码、Google+ 用户 ID 保存在数据库中。 但在第二种情况下,即查找用户好友列表,我再次获得用户必须授权应用程序的屏幕。

有人可以帮我吗?

另外是否可以在离线模式下获取所有用户详细信息,例如使用数据库中的身份验证令牌、代码、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');

【问题讨论】:

    标签: php google-api google-plus


    【解决方案1】:

    一般来说,在离线模式下工作时,您需要存储从服务器返回的整个对象 - 而不仅仅是身份验证令牌。此对象包括长期刷新令牌以及有关何时需要进行刷新的信息。将整个令牌传递给服务允许它为您刷新令牌。

    您想出的答案看起来像是您自己手动刷新 - 这很好,但通常没有必要。

    在某些情况下,如果用户在您请求离线模式之前授权您的应用,而您后来又请求离线模式,您仍然不会获得刷新令牌。谷歌只会在被问及是否没有授权令牌(当前或过期)时提供刷新令牌。有两种解决方法:

    1. 当您切换到使用离线模式时,请使您的旧客户端 ID/密码无效并开始使用新的。
    2. 让用户通过https://accounts.google.com/b/0/IssuedAuthSubTokens 或(如果您使用 Google+ 登录)通过https://plus.google.com/apps 取消对您的应用的授权

    【讨论】:

    • 对不起,我没有得到你的最后一点。你能详细说明一下吗?
    【解决方案2】:

    终于搞定了

    在好友列表页面上:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      相关资源
      最近更新 更多