Gmail从我的网站登录 [英] Gmail Login from my site

查看:105
本文介绍了Gmail从我的网站登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试在我的网站上登录gmail。我使用了Oauth2。

http://www.idiotminds.com/sign-in-with-google-account-using-oauth2-0/



这里的流程工作正常,但我没有从服务器获取任何数据,如用户名,电子邮件等。没有任何数据返回给我



http://22yardscricket.com/gmaillogin/index.php





这是我的代码:

config.php:



<?php 
/ *
-------------------------------------------------- ----
www.idiotminds.com
--------------------------------- -----------------------
* /
session_start();

$ base_url = filter_var(' http://22yardscricket.com/',FILTER_SANITIZE_URL) ;

//访问https://code.google.com/apis/console 生成
// oauth2_client_id,oauth2_client_secret, 注册您的oauth2_redirect_uri。
define(' CLIENT_ID','883082907275-qgecuunt11au1pj49l5o0qn4prjks8oh.apps.googleusercontent.com');
define(' CLIENT_SECRET','4JYX7-AFq5xui7GIpYhS56ck');
define(' REDIRECT_URI','http://22yardscricket.com/gmaillogin/return.php');
define(' APPROVAL_PROMPT','auto');
define( ' ACCESS_TYPE','online');
?>





index.php:

 <?  php     require_once(../ classes / lib.php);? >  
< span class =code-summarycomment><? php
< span class =code-summarycomment>
/ *

--------------------- ---------------------------------

www.idiotminds.com

---------------------------------- ----------------------

* /

require_once 'config.php';

require_once 'lib / Google_Client.php';

require_once 'lib / Google_Oauth2Service.php';



$ client = new Google_Client() ;

$ client - > setApplicationName(Google UserInfo PHP Starter Application);

$ client-> setClientId(CLIENT_ID);
$ client-> setClientSecret(CLIENT_SECRET);
$ client-> setRedirectUri(REDIRECT_URI);
$ client-> setApprovalPrompt(APPROVAL_PROMPT);
$ client-> setAccessType(ACCESS_TYPE);

$ oauth2 =新的Google_Oauth2Service($ client);

if(isset($ _ GET ['code'])){
$ client-> authenticate($ _ GET ['code']);
$ _SESSION ['token'] = $ client-> getAccessToken();
echo'< script 类型 = text / javascript > window.close(); < / script > ';出口;
}

if(isset($ _ SESSION ['token'])){
$ client-> setAccessToken($ _ SESSION ['token']);
}

if(isset($ _ REQUEST ['error'])){
echo'< < span class =code-leadattribute> script type = text / javascript > window.close(); < span class =code-keyword>< / script > ';出口;
}

if($ client-> getAccessToken()){
$ user = $ oauth2-> userinfo-> get();

$ sql =INSERT INTO用户(姓名,电子邮件,性别)VALUES('。$ user ['name']。','。$ user ['email']。 ''。$用户[ '性别'。 ');
mysql_query($ sql);

//这些字段目前通过PHP清理过滤器进行过滤。
//参见http://www.php.net/manual/en/filter.filters.sanitize.php
$ email = filter_var($ user ['email'],FILTER_SANITIZE_EMAIL);
$ img = filter_var($ user ['picture'],FILTER_VALIDATE_URL);
$ personMarkup =$ email < div > < img src =' $ img?sz = 50' > < / div > ;

$ _SESSION [email] = $ email;
$ _SESSION [name] = $ user ['name'];

echo< script > alert('。$ email。'); < / script > ;

//访问令牌可能已经懒惰更新。
$ _SESSION ['token'] = $ client-> getAccessToken();

} else {
$ authUrl = $ client-> createAuthUrl();
}
?>
< !doctype html >
< html >
< head > < < span class =code-leadattribute> meta charset = utf-8 >
< title > 使用Google帐户登录 - 白痴头脑< / title >
< script src = http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js 类型 = text / javascript > < / script >
< script 类型 = text / javascript src = js / oauthpopup.js > < / script >
< script 类型 = text / javascript >
$(document).ready(function(){

$('a.login')。oauthpopup({
path:'< span class =code-summarycomment><? php if(isset($ authUrl)){echo $ authUrl;} else { echo '';}? > ',
宽度:650,
高度:350,
});
$('a.logout')。googlelogout({
redirect_url:'<? php echo $ base_url; > logout.php'
});

});
< / script >

< / head >
< body >
< div style = float :left; width:33%; margin-left:375px; >
< ; php if(isset($ personMarkup)): >
<? php < span class =code-summarycomment> print $ personMarkup >
<? php endif >
<? php < span class =code-summarycomment>

< span class = code-summarycomment> if(isset($ authUrl)) {

print < a class =' login' href =' javascript:void(0);' > < <跨度class =code-leadattribute> img alt =' 使用Google登录 src =' signin_google.png' / > < / a > ;
} else {
print< a class =' logout ' href =' javascript:void(0);' > 退出< ; / a > ;
}
?>
< / div >

< / body > < / html >





请建议。





问候,

Harish

解决方案

base_url = filter_var(' http://22yardscricket.com/',FILTER_SANITIZE_URL);

//访问https://code.google.com / apis / console 生成
// oauth2_client_id,oauth2_client_secret, 注册你的oauth2_redirect_uri。
define(' CLIENT_ID','883082907275-qgecuunt11au1pj49l5o0qn4prjks8oh.apps.googleusercontent.com');
define(' CLIENT_SECRET','4JYX7-AFq5xui7GIpYhS56ck');
define(' REDIRECT_URI','http://22yardscricket.com/gmaillogin/return.php');
define(' APPROVAL_PROMPT','auto');
define( ' ACCESS_TYPE','online');
?>





index.php:

 <?  php     require_once(../ classes / lib.php);? >  
< span class =code-summarycomment><? php
< span class =code-summarycomment>
/ *

--------------------- ---------------------------------

www.idiotminds.com

---------------------------------- ----------------------

* /

require_once 'config.php';

require_once 'lib / Google_Client.php';

require_once 'lib / Google_Oauth2Service.php';




client = new Google_Client();


client - > setApplicationName(Google UserInfo PHP Starter申请);

Hello,

I'm trying gmail login on my website. i used Oauth2 for this.
http://www.idiotminds.com/sign-in-with-google-account-using-oauth2-0/

Here process is working fine but i don't getting any data from the service like username , email etc. no any data is returning for me

http://22yardscricket.com/gmaillogin/index.php


Here is my code:
config.php:

<?php
/*
------------------------------------------------------
  www.idiotminds.com
--------------------------------------------------------
*/
session_start();

$base_url= filter_var('http://22yardscricket.com/', FILTER_SANITIZE_URL);

// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
define('CLIENT_ID','883082907275-qgecuunt11au1pj49l5o0qn4prjks8oh.apps.googleusercontent.com');
define('CLIENT_SECRET','4JYX7-AFq5xui7GIpYhS56ck');
define('REDIRECT_URI','http://22yardscricket.com/gmaillogin/return.php');
define('APPROVAL_PROMPT','auto');
define('ACCESS_TYPE','online');
?>



index.php:

<?php require_once("../classes/lib.php");?>
<?php

/*

------------------------------------------------------

  www.idiotminds.com

--------------------------------------------------------

*/

require_once 'config.php';

require_once 'lib/Google_Client.php';

require_once 'lib/Google_Oauth2Service.php';



$client = new Google_Client();

$client->setApplicationName("Google UserInfo PHP Starter Application");

$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setApprovalPrompt(APPROVAL_PROMPT);
$client->setAccessType(ACCESS_TYPE);

$oauth2 = new Google_Oauth2Service($client);

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  echo '<script type="text/javascript">window.close();</script>'; exit;
}

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['error'])) {
 echo '<script type="text/javascript">window.close();</script>'; exit;
}

if ($client->getAccessToken()) {
  $user = $oauth2->userinfo->get();

    $sql="INSERT INTO users (name,email,gender) VALUES ('".$user['name']."','".$user['email']."','".$user['gender']."')";
    mysql_query($sql);

  // These fields are currently filtered through the PHP sanitize filters.
  // See http://www.php.net/manual/en/filter.filters.sanitize.php
  $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
  $img = filter_var($user['picture'], FILTER_VALIDATE_URL);
  $personMarkup = "$email<div><img src='$img?sz=50'></div>";

$_SESSION["email"] = $email;
$_SESSION["name"] = $user['name'];

echo "<script>alert('".$email."');</script>";

  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();

} else {
  $authUrl = $client->createAuthUrl();
}
?>
<!doctype html>
<html>
<head><meta charset="utf-8">
    <title>Signin with Google Account-Idiot Minds</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/oauthpopup.js"></script>
<script type="text/javascript">
$(document).ready(function(){

     $('a.login').oauthpopup({
            path: '<?php if(isset($authUrl)){echo $authUrl;}else{ echo '';}?>',
            width:650,
            height:350,
        });
        $('a.logout').googlelogout({
            redirect_url:'<?php echo $base_url; ?>logout.php'
        });

});
</script>

</head>
<body>
 <div style="float:left;width:33%;margin-left:375px;">
<?php if(isset($personMarkup)): ?>
<?php print $personMarkup ?>
<?php endif ?>
<?php

  if(isset($authUrl)) {

    print "<a class='login' href='javascript:void(0);'><img alt='Signin in with Google' src='signin_google.png'/></a>";
  } else {
   print "<a class='logout' href='javascript:void(0);'>Logout</a>";
  }
?>
</div>

</body></html>



kindly suggest.


Regards,
Harish

解决方案

base_url= filter_var('http://22yardscricket.com/', FILTER_SANITIZE_URL); // Visit https://code.google.com/apis/console to generate your // oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri. define('CLIENT_ID','883082907275-qgecuunt11au1pj49l5o0qn4prjks8oh.apps.googleusercontent.com'); define('CLIENT_SECRET','4JYX7-AFq5xui7GIpYhS56ck'); define('REDIRECT_URI','http://22yardscricket.com/gmaillogin/return.php'); define('APPROVAL_PROMPT','auto'); define('ACCESS_TYPE','online'); ?>



index.php:

<?php require_once("../classes/lib.php");?>
<?php

/*

------------------------------------------------------

  www.idiotminds.com

--------------------------------------------------------

*/

require_once 'config.php';

require_once 'lib/Google_Client.php';

require_once 'lib/Google_Oauth2Service.php';




client = new Google_Client();


client->setApplicationName("Google UserInfo PHP Starter Application");


这篇关于Gmail从我的网站登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆