一、申请clientID

https://console.developers.google.com/project

二、开启Google+ API权限

https://console.developers.google.com/project/gentle-charmer-848/apiui/api

三、添加同意画面

四、建立新的用户端ID(一个域名对应一个ID)

五、显示页面js登录按钮

案例地址:https://developers.google.com/+/web/signin/

参考代码:
                <button id="customBtn" class="customGPlusSignIn"><b><img src="data:images/btn_red_32.png" width="32" height="32"/></b>Log in with Google</button>
                <script type="text/javascript">
                function render() {
                    gapi.signin.render('customBtn', {
                      'callback': 'signinCallback',
                      'approvalprompt':'force',
                      'clientid': '779557060237-b9fqq4gnr5qchdij2j8h5h13ujla52fj.apps.googleusercontent.com',
                      'cookiepolicy': 'http://www.5dlj.com',
                      'requestvisibleactions': 'http://schemas.google.com/AddActivity',
                      'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'
                });}
                function signinCallback(authResult) {
                  if (authResult['access_token']) {
                        gapi.client.load("oauth2","v2",function(){
                            var request=gapi.client.oauth2.userinfo.get();
                            request.execute(function(obj){
                                if(obj["email"] == ""){
                                    alert('Email is empty!');
                                }
                            });
                        });
                    window.location.href="google_login.php?access_token="+authResult['access_token'];
                  } else if (authResult['error']) {
                    authResult['error'];
                  }
                }
                !function() {
                    var po = document.createElement('script');
                    po.type = 'text/javascript'; po.async = true;
                    po.src = 'https://apis.google.com/js/client:plusone.js?onload=render';
                    var s = document.getElementsByTagName('script')[0];
                    s.parentNode.insertBefore(po, s);
                }();
                </script>

六、google_login.php返回结果处理

<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

if(isset($_REQUEST['access_token'])) {
    $access_token = $_REQUEST['access_token'];
    $url = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=".$access_token;

$user_gg = json_decode(file_get_contents($url),true);
    $gg_email = compile_str($user_gg['email']);
    $gg_id    = $user_gg['id'];
    $gg_name  = compile_str($user_gg['name']);
    $picture  = $user_gg['picture'] == 'https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg' ? '' : compile_str($user_gg['picture']); //头像
    $gender   = 0;
    if($user_gg['gender'] == 'male')//性别
    {
        $gender = 1;
    }
    elseif($user_gg['gender'] == 'female')
    {
        $gender = 2;
    }

$locale   = compile_str($user_gg['locale']); //国家
    $verified_email  = $user_gg['verified_email']; //邮箱是否验证
    if(empty($gg_email))
    {
        show_message("Your Google email is empty!", '', 'user.php', 'warning');
    }
    
    $sql = "select * from ".$GLOBALS['ecs']->table("users")." where email='".$gg_email.
    "' or user_name='".$gg_email."' order by user_id desc limit 1";
    $user_info = $GLOBALS['db']->getRow($sql);
    $record = array();
    if(!empty($user_info)) //gg邮箱已注册或者登陆过
    {
        $user_name = $user_info['user_name'];
        $user->set_session($user_name);
        $user->set_cookie($user_name, null);
        update_user_info();
        recalculate_price();
        
        if(empty($user_info['gg_id']) || empty($user_info['nick_name']) || empty($user_info['user_image']) || empty($user_info['sex']))
        {
            $record['is_validated'] = 1;
            empty($user_info['gg_id']) ? $record['gg_id'] = $gg_id : '';
            empty($user_info['gg_name']) ? $record['gg_name'] = $gg_name : '';
            empty($user_info['nick_name']) ? $record['nick_name'] = $gg_name : '';
            empty($user_info['user_image']) ? $record['user_image'] = get_picture($picture) : '';
            empty($user_info['sex']) ? $record['sex'] = $gender : '';
            $db->autoExecute($ecs->table('users'), $record, 'UPDATE', "user_id = '$user_info[user_id]'");
        }
    }
    else //GG邮箱未注册或者未登陆过,自动注册
    {
        $password = generate_word();
        include_once(ROOT_PATH."includes/lib_passport.php");
        include_once(ROOT_PATH.'includes/lib_transaction.php');
        $other = array();
        if(register($gg_email, $password, $gg_email,$other))
        {
            $record['gg_id']      = $gg_id;
            $record['gg_name']    = $gg_name;
            $record['nick_name']  = $gg_name;
            $record['user_image'] = get_picture($picture);
            $record['sex']        = $gender;
            $record['reg_type']   = 3;
            $record['is_validated']= 1;
            $db->autoExecute($ecs->table('users'), $record, 'UPDATE', "user_id = '$_SESSION[user_id]'");
            /* 发送注册成功邮件*/
            $tpl = get_mail_template('pp_login');
            $expired_date =local_date("Y-m-d", gmtime()+864000);
            $smarty->assign('expired_date', $expired_date);
            $smarty->assign('password', $password);
            $smarty->assign('username', $gg_email);
            $content = $GLOBALS['smarty']->fetch('str:' . $tpl['template_content']);
            send_mail($gg_email, $gg_email,$tpl['template_subject'],$content,$tpl['is_html']);
            log_account_change($_SESSION['user_id'], 0, 0, 0, 50, 'Get 50 M points from Google register.', ACT_OTHER);
        }
        else
        {
            show_message("Your Google email register error!", '', 'user.php', 'warning');
        }
    }
    $_SESSION['back_act'] = $_SESSION['back_act'] ? $_SESSION['back_act'] : "./index.html";
    ecs_header("Location:".$_SESSION['back_act']."\n");
}
else
{
    die('Illegal Access!');
}
//获取远程头像图片
function get_picture($picture){
    if($picture)
    {
        $data = file_get_contents($picture); // 读文件内容
        $filename =  DATA_DIR . '/u_image/'.local_date('ymdHis',gmtime()).'_'.rand(10,99).substr($picture,-4,4); //得到时间戳
        $fp = @fopen(ROOT_PATH . $filename,"w"); //以写方式打开文件
        @fwrite($fp,$data);
        fclose($fp);
    }
    else
    {
        $filename = '';    
    }
    return $filename;
}
?>
七、添加新字段的sql语句

ALTER TABLE `ecs_users` ADD `gg_name` varchar(60) NOT NULL;
ALTER TABLE `ecs_users` ADD `gg_id` varchar(60) NOT NULL;
ALTER TABLE `ecs_users` ADD `nick_name` varchar(60) NOT NULL;
ALTER TABLE `ecs_users` ADD `user_image` varchar(60) NOT NULL;

ecshop使用Google API及OAuth2.0登录授权(PHP)的更多相关文章

  1. Spring Security OAuth2.0认证授权六:前后端分离下的登录授权

    历史文章 Spring Security OAuth2.0认证授权一:框架搭建和认证测试 Spring Security OAuth2.0认证授权二:搭建资源服务 Spring Security OA ...

  2. Force.com微信开发系列(七)OAuth2.0网页授权

    OAuth是一个开放协议,允许用户让第三方应用以安全且标准的方式获取该用户在某一网站上存储的私密资源(如用户个人信息.照片.视频.联系人列表),而无须将用户名和密码提供给第三方应用.本文将详细介绍OA ...

  3. 使用Owin中间件搭建OAuth2.0认证授权服务器

    前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...

  4. ***微信公众平台开发: 获取用户基本信息+OAuth2.0网页授权

    本文介绍如何获得微信公众平台关注用户的基本信息,包括昵称.头像.性别.国家.省份.城市.语言.本文的方法将囊括订阅号和服务号以及自定义菜单各种场景,无论是否有高级接口权限,都有办法来获得用户基本信息, ...

  5. Spring Cloud Zuul 网关使用与 OAuth2.0 认证授权服务

    API 网关的出现的原因是微服务架构的出现,不同的微服务一般会有不同的服务地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题: 客户端会 ...

  6. 微信OAuth2.0网页授权

    1.OAuth2.0网页授权 关于网页授权的两种scope的区别说明 1.以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页 ...

  7. 微信公众平台开发(71)OAuth2.0网页授权

    微信公众平台开发 OAuth2.0网页授权认证 网页授权获取用户基本信息 作者:方倍工作室 微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使 ...

  8. Owin中间件搭建OAuth2.0认证授权服务体会

    继两篇转载的Owin搭建OAuth 2.0的文章,使用Owin中间件搭建OAuth2.0认证授权服务器和理解OAuth 2.0之后,我想把最近整理的资料做一下总结. 前两篇主要是介绍概念和一个基本的D ...

  9. 微信公众平台开发-OAuth2.0网页授权(含源码)

    微信公众平台开发-OAuth2.0网页授权接口.网页授权接口详解(含源码)作者: 孟祥磊-<微信公众平台开发实例教程> 在微信开发的高级应用中,几乎都会使用到该接口,因为通过该接口,可以获 ...

随机推荐

  1. http://www.sufeinet.com/thread-655-1-1.html

    http://www.sufeinet.com/thread-655-1-1.html

  2. UVALive 6959 - Judging Troubles

    给两组字符串,最多有多少对相同. map做映射判断一下. #include <iostream> #include <cstdio> #include <map> ...

  3. 2015.4.10-C#入门基础(三)

    今天,我们聊一聊一些基本问题: 1.修饰符有哪些?有什么区别呢? 首先大家想到的应该是 public:特点是所属类的成员和非所属类的成员都可以访问 private:只有所属类的成员才可以访问 prot ...

  4. 常用工具类,文件和内存的大小获取,shell脚本的执行

    /* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  5. android.view.InflateException: Binary XML file line #7: Error inflating class(OOM)

    由于页面含有ImageView引起的内存溢出. 作如下处理:在OnDestroy中 Drawable d = imageView.getDrawable(); if (d != null) d.set ...

  6. 关于twitter的GIF变mp4的测试

    这个事是好久之前听说了,今天FQ的时候突然想起来了,就去测试了一下这个gif转MP4到底有多神奇... 这个是我的twitter地址:https://twitter.com/chendatony31 ...

  7. hdoj分类

    http://blog.csdn.net/lyy289065406/article/details/6642573 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 ...

  8. JSP使用JDBC ODBC 实例

    1.JDBC ODBC Brige driver是JSP连接数据库的驱动,只要安装了JDK这个驱动就默认安装了 2. 配置JDBC ODBC数据源的步骤:http://blog.csdn.net/li ...

  9. mysql应用技巧

    1. 查看mysql死锁 SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX; 2.查看正在锁的事务 SELECT * FROM INFORMATION_SCHEM ...

  10. Unix/Linux环境C编程入门教程(24) MySQL 5.7.4 for Red Hat Enterprise 7(RHEL7)的安装

    远观历史, MySQL的主要目的是为了能够在单处理器核心的商业服务器上运行.如今MySQL的一个变化用户可能不会注意到,那就是甲骨文已经开始重新架构MySQL的代码,使它大量的模块化.如软件解析器,优 ...