PHP头条
热点:

Config.php

在这里,你必须配置OAuth应用密钥和用户密钥。

  1. // OAuth2 Settings, you can get these keys at https://code.google.com/apis/console Step 6 keys  
  2. 'oauth2_client_id' => 'App Client ID',  
  3. 'oauth2_client_secret' => 'App Client Secret',  
  4. 'oauth2_redirect_uri' => 'http://yoursite.com/gplus/index.php',  
  5.  
  6. // OAuth1 Settings Step 3  keys.  
  7. 'oauth_consumer_key' => 'OAuth Consumer Key',  
  8. 'oauth_consumer_secret' => 'OAuth Consumer Secret'

gplus_login.php

google+的登录系统。

  1. <?php  
  2. require_once 'src/apiClient.php';  
  3. require_once 'src/contrib/apiPlusService.php';  
  4. session_start ();  
  5. $client = new apiClient ();  
  6. $client->setApplicationName ( "9lessons Google+ Login Application" );  
  7. $client->setScopes ( array ('https://www.googleapis.com/auth/plus.me' ) );  
  8. $plus = new apiPlusService ( $client );  
  9. if (isset ( $_REQUEST ['logout'] )) {  
  10.     unset ( $_SESSION ['access_token'] );  
  11. }  
  12.  
  13. if (isset ( $_GET ['code'] )) {  
  14.     $client->authenticate ();  
  15.     $_SESSION ['access_token'] = $client->getAccessToken ();  
  16.     header ( 'Location: http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['PHP_SELF'] );  
  17. }  
  18.  
  19. if (isset ( $_SESSION ['access_token'] )) {  
  20.     $client->setAccessToken ( $_SESSION ['access_token'] );  
  21. }  
  22.  
  23. if ($client->getAccessToken ()) {  
  24.     $me = $plus->people->get ( 'me' );  
  25.     $_SESSION ['access_token'] = $client->getAccessToken ();  
  26. else 
  27.     $authUrl = $client->createAuthUrl ();  
  28.  
  29. if (isset ( $me )) {  
  30.     $_SESSION ['gplusdata'] = $me;  
  31.     header ( "location: home.php" );  
  32. }  
  33.  
  34. if (isset ( $authUrl ))  
  35.     print "<a class='login' href='$authUrl'>Google Plus Login </a>";  
  36. else 
  37.     print "<a class='logout' href='index.php?logout'>Logout</a>";  
  38. ?> 

home.php

这里包含了将google+的session信息插入user数据表的PHP代码。

  1. <?php  
  2. session_start();  
  3. if (!isset($_SESSION['gplusdata'])) {  
  4.     // Redirection to home page  
  5.     header("location: index.php");  
  6. }else{  
  7.     $me=$_SESSION['gplusdata'];  
  8.     echo "<img src='{$me['image']['url']}'/>";  
  9.     echo "Name: {$me['displayName']}";  
  10.     echo "Gplus Id:  {$me['id']}";  
  11.     echo "Male: {$me['gender']}";  
  12.     echo "Relationship: {$me['relationshipStatus']}";  
  13.     echo "Location: {$me['placesLived'][0]['value']}";  
  14.     echo "Tagline: {$me['tagline']}";  
  15.     print "<a class='logout' href='index.php?logout'>Logout</a> ";  
  16. }  
  17. ?> 

下面附上使用Google Plus Oauth登录的示例源码:下载点这里

原文链接:http://www.phpfuns.com/php/login-with-google-plus-oauth.shtml

编辑推荐】


www.phpzy.comtrue/php/3731.htmlTechArticleConfig.php 在这里,你必须配置OAuth应用密钥和用户密钥。 //OAuth2Settings,youcangetthesekeysathttps://code.google.com/apis/consoleStep6keys 'oauth2_client_id' = 'AppClientID' , 'oauth...

相关文章

相关频道:

PHP之友评论

今天推荐