社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
前阵子Google+已经发布了OAuth的应用程序编程接口,现在他们只提供用户活动和循环数据。我已经使用PHP通过Google+数据简单实现了一个称为用户身份验证的登录系统。试试这几乎就像twitter登录系统,我希望未来Google+会释放更多选项。
使用Google Plus Oauth登录
第一步
点击这里添加或者注册你的域名。
添加或者注册你的域名
第二步
通过HTML文件上传或包括META标签来验证您的域名所有权。
通过HTML文件上传或包括META标签来验证您的域名所有权。
第三步
谷歌将会提供你OAuth用户密钥和秘密密钥。
谷歌将会提供你OAuth用户密钥和秘密密钥
第四步
在Oauth控制台创建客户端ID(Client ID)。
在Oauth控制台创建客户端ID(Client ID)
在Oauth控制台创建客户端ID(Client ID)
第五步
应用的Oauth Client ID和客户端密钥(client secret)。
应用的Oauth Client ID和客户端密钥(client secret)
下面来看一下我们的程序文件。
Config.php
在这里,你必须配置OAuth应用密钥和用户密钥。
- // OAuth2 Settings, you can get these keys at https://code.google.com/apis/console Step 6 keys
- 'oauth2_client_id' => 'App Client ID',
- 'oauth2_client_secret' => 'App Client Secret',
- 'oauth2_redirect_uri' => 'http://yoursite.com/gplus/index.php',
- // OAuth1 Settings Step 3 keys.
- 'oauth_consumer_key' => 'OAuth Consumer Key',
- 'oauth_consumer_secret' => 'OAuth Consumer Secret',
gplus_login.php
google+的登录系统。
- <?php
- require_once 'src/apiClient.php';
- require_once 'src/contrib/apiPlusService.php';
- session_start ();
- $client = new apiClient ();
- $client->setApplicationName ( "9lessons Google+ Login Application" );
- $client->setScopes ( array ('https://www.googleapis.com/auth/plus.me' ) );
- $plus = new apiPlusService ( $client );
- if (isset ( $_REQUEST ['logout'] )) {
- unset ( $_SESSION ['access_token'] );
- }
- if (isset ( $_GET ['code'] )) {
- $client->authenticate ();
- $_SESSION ['access_token'] = $client->getAccessToken ();
- header ( 'Location: http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['PHP_SELF'] );
- }
- if (isset ( $_SESSION ['access_token'] )) {
- $client->setAccessToken ( $_SESSION ['access_token'] );
- }
- if ($client->getAccessToken ()) {
- $me = $plus->people->get ( 'me' );
- $_SESSION ['access_token'] = $client->getAccessToken ();
- } else
- $authUrl = $client->createAuthUrl ();
- if (isset ( $me )) {
- $_SESSION ['gplusdata'] = $me;
- header ( "location: home.php" );
- }
- if (isset ( $authUrl ))
- print "<a class='login' href='$authUrl'>Google Plus Login </a>";
- else
- print "<a class='logout' href='index.php?logout'>Logout</a>";
- ?>
home.php
这里包含了将google+的session信息插入user数据表的PHP代码。
- <?php
- session_start();
- if (!isset($_SESSION['gplusdata'])) {
- // Redirection to home page
- header("location: index.php");
- }else{
- $me=$_SESSION['gplusdata'];
- echo "<img src='{$me['image']['url']}'/>";
- echo "Name: {$me['displayName']}";
- echo "Gplus Id: {$me['id']}";
- echo "Male: {$me['gender']}";
- echo "Relationship: {$me['relationshipStatus']}";
- echo "Location: {$me['placesLived'][0]['value']}";
- echo "Tagline: {$me['tagline']}";
- print "<a class='logout' href='index.php?logout'>Logout</a> ";
- }
- ?>
下面附上使用Google Plus Oauth登录的示例源码:下载点这里
原文链接:http://www.phpfuns.com/php/login-with-google-plus-oauth.shtml
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!