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
编辑推荐】
PHP之友评论