php – 如何使用Google OAuth获取Google联系人信息?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何使用Google OAuth获取Google联系人信息?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我是OAuth的新手,想要创建一个页面,使用OAuth系统从GOOGLE获取用户的联系人列表,以便他们不必登录.

我该如何做?我正在使用PHP,所以我真的很感激,如果有这样的示例代码.我似乎无法在Google上找到它.

请帮忙!

谢谢

对于访问Google的一般OAuth原则,您可能会发现 Google’s OAuth playground非常有用(联系人包含在那里).

这是一个非常基本的例子(使用PHP oauth PEcl扩展名和simpleXMl,它只打印出25个第一个联系人的名称):

<?PHP
$cons_key="your consumer key";
$cons_sec="your consumer secret";

$callback="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$req_token_url="https://www.google.COM/accounts/OAuthGetRequestToken";
$auth_token_url="https://www.google.com/accounts/OAuthAuthorizeToken";
$acc_token_url="https://www.google.com/accounts/OAuthGetAccessToken";

$scope="https://www.google.com/m8/Feeds/";
$scopes=urlencode($scope);
$req_scope_token_url=$req_token_url."?scope=".$scopes;
$endpoint="https://www.google.com/m8/Feeds/contacts/default/full/";

session_start();

if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;

try {
    $oauth = new OAuth($cons_key,$cons_sec);
    if(!isset($_GET['oauth_token']) &amp;& !$_SESSION['state']) {
        $oauth = new OAuth($cons_key,$cons_sec);
        $oauth->setRequestEngine(OAUTH_REQENGINE_CURL);
        $request_token_info = $oauth->getRequestToken($req_scope_token_url,$callback);
        if(!empty($request_token_info)) {
            $_SESSION['token']=$request_token_info['oauth_token'];
            $_SESSION['secret']=$request_token_info['oauth_token_secret'];
            $_SESSION['state']=1;
            header('Location: '.$auth_token_url.'?oauth_token='.$_SESSION['token']);
            exIT;
        }
    } else if($_SESSION['state']==1) {
        $oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
        $access_token_info = $oauth->getAccessToken($acc_token_url);
        $_SESSION['state'] = 2;
        $_SESSION['token'] = $access_token_info['oauth_token'];
        $_SESSION['secret'] = $access_token_info['oauth_token_secret'];
    }

    $oauth->fetch($endpoint);
    parseAtom($oauth->getLastResponse());

} catch(oauthexception $E) {
    PRint_r($E);
}

function parseAtom($atomstring) {
    global $oauth;
    $atom=simplexML_load_string($atomstring);
    foreach ($atom->entry as $entry) {
        print $entry->title.",";
    }
}
?>

你可以看到上面的代码in action here.

安装(配置)oauth pecl扩展名可能很棘手,您可以使用may have to check your php.ini and/or specify a requestengine.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何使用Google OAuth获取Google联系人信息?全部内容,希望文章能够帮你解决php – 如何使用Google OAuth获取Google联系人信息?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。