duandong9195 2014-08-26 17:41
浏览 51
已采纳

Google+:在离线模式下获取朋友的好友列表

I want to find the list of friends of Google+ user who have authorized the app without asking for authorization again.

For example: Once the user login using the Google+ to my website, I store the auth token, code, Google+ user id in the db. So once that is done I want to find the list of users friends in his circle. I managed to get the first part working and it saves the auth token, code, Google+ user id in the db. But in the second case i.e. finding the list of users friends, I again get a the screen where user have to authorize the app.

Can someone please help me in this?

Also is it possible to get all the user details in offline mode like using the auth token, code, Google+ user id which is in the db?

Initial code(login):

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

In callback page:

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

Friends listing page:

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');
  • 写回答

2条回答 默认 最新

  • douzongluo7542 2014-08-27 09:44
    关注

    Finally managed to get get it done

    On Friends listing page:

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?