第三方Google登录

1、获取客户端ID(https://console.developers.google.com/apis/credentials),如下图:




2、加载Google API平台库以创建gapi对象(https://apis.google.com/js/platform.js):



ps:这里面要注意,使用Google API的之前要在谷歌后台启用people API库,如图:


点击启用即可。

3、上述配置完成后,就可以开始接sdk了,首先加载auth2库:

function onInitGoogle (opts, sccess, error) {
  // let getAuthInstance = {};
  let baseOptions = {
    client_id: '你的客户端ID',
    cookiepolicy: 'single_host_origin'
  };
  let options = {...baseOptions, ...opts};
  gapi.load ('auth2', function () {
    gapi.auth2.init (options).then (res => {
      sccess (res);
    }, err => {
      error (err);
    });
  });
}复制代码

一、gapi.load()方法:是加载auth2的库,左右方法要在此方法加载完成后才可以用。

二、gapi.auth2.init()方法:初始化GoogleAuth对象,该方法是为后面的             gapi.auth2.getAuthInstance方法的初始化。

4、初始化操作之后调用gapi.auth2.getAuthInstance方法,这个方法可以让你获取用户的基本信息,当前的登录状态及注销等基本操作

function getAuthInstance () {
  return gapi.auth2.getAuthInstance ();
}复制代码

5、Google登录

/**
 *google登录
 */
function googleSignIn (success, error) {
  this.getAuthInstance.signIn ().then (function (res) {
    success (res);
  }, function (err) {
    error (err);
  });
}复制代码

一、这里面的this.getAuthInstance方法就是步骤4定义的方法;

二、这里面的then是google自己封装的promise,直接then就可以获取返回的结果。有两个参数,onInit和onError;

最后附上一个完成的调用方法示例,详细的方法参考(https://developers.google.com/identity/sign-in/web/reference#gapiauth2authresponse):

//ThirdPartyLogin.js
/**
 * 初始化谷歌登录
 * @param opts
 * @param sccess
 * @param error
 */
function onInitGoogle (opts, sccess, error) {
  // let getAuthInstance = {};
  let baseOptions = {
    client_id: '你的客户端ID',
    cookiepolicy: 'single_host_origin'
  };
  let options = {...baseOptions, ...opts};
  gapi.load ('auth2', function () {
    gapi.auth2.init (options).then (res => {
      sccess (res);
    }, err => {
      error (err);
    });
  });
}

/**
 * 初始化谷歌的getAuthInstance
 * GoogleAuth 是一个单例类,提供允许用户使用Google帐户登录,获取用户当前登录状态,从用户的Google个人资料中获取特定数据,请求其他范围以及从当前帐户注销的方法。
 * @returns {*}
 */
function getAuthInstance () {
  return gapi.auth2.getAuthInstance ();
}

/**
 *google登录
 */
function googleSignIn (success, error) {
  this.getAuthInstance.signIn ().then (function (res) {
    success (res);
  }, function (err) {
    error (err);
  });
}

const ThirdPartyLogin = {
  onInitGoogle,
  getAuthInstance,
  googleSignIn
};

export default ThirdPartyLogin;
复制代码

//index.js
import ThirdPartyLogin from './ThirdPartyLogin';

class Platform {
  constructor () {
    this.onInit ();
  }

  onInit () {
    this.onInitEvent ();
    this.onInitDomEvent ();
    this.onInitGoogleEvent ();//初始化谷歌事件
}
  onInitEvent () {//订阅google登录事件
    $.subscribe ('onGoogleLoginEvent', this.onGoogleLoginEvent.bind (this));
  }

  onInitDomEvent () {//点击google登录发布事件
    $ ('#gl-login-button').click (function () {
      $.publish ('onGoogleLoginEvent');
    });
  }

  /**
   * Google事件
   */
  onGoogleLoginEvent () {
    if (this.googleInfo.isSignedIn) {
      this.googleCallbackHandler (ThirdPartyLogin.getAuthInstance ());//已经登录的回调
    } else {
      ThirdPartyLogin.googleSignIn ((res) => {//执行google的登录方法
        //登录成功后的方法
        this.googleCallbackHandler (ThirdPartyLogin.getAuthInstance ());
      }, (err) => {
        console.log (err.error);
      });
    }
  }

  /**
   * google初始化事件
   */
  onInitGoogleEvent () {//初始化谷歌事件
    ThirdPartyLogin.onInitGoogle ({}, (res) => {
      console.log ('google init complete...', res);
      let getAuthInstance = ThirdPartyLogin.getAuthInstance ();//获取GoogleAuth对象
      this.googleInfo.isSignedIn = getAuthInstance.isSignedIn.get ();//存储登录状态
    }, function (err) {
      return T.toast (err.details);
    });
  }

  /**
   * 谷歌登录成功的回调
   */
  googleCallbackHandler (getAuthInstance) {
    console.log ('登录成功', getAuthInstance);//这个参数是GoogleAuth对象
    this.googleInfo.isSignedIn = getAuthInstance.isSignedIn.get ();//存储登录状态
    console.log(this.googleInfo);
    let GoogleUser = getAuthInstance.currentUser.get ();//这个方法获取返回的响应对象
    console.log ('响应对象:', GoogleUser.getAuthResponse (true));//true参数可以用于获取access_token
  }

}

let platform = new Platform ();
复制代码


转载于:https://juejin.im/post/5c20aca05188251f64304d57

谷歌第三方登录是一种利用谷歌账号登录第三方网站或应用程序的方式。在 PHP 中,我们可以使用谷歌提供的 OAuth 2.0 协议来实现这一功能。 首先,我们需要在谷歌开发者控制台上创建一个项目,并获取到谷歌提供的客户端ID和客户端密钥。接下来,我们需要安装并引入谷歌 PHP 客户端库。 在网站或应用程序中,我们可以提供一个链接或按钮,让用户点击以登录谷歌账号。当用户点击该链接时,我们将向谷歌发送一个授权请求,并重定向用户到谷歌登录页面。用户需要登录并授权我们的应用程序的访问权限。 一旦用户授权成功,谷歌将会将一个授权码发送回我们的重定向URL。我们可以通过在该URL中检索授权码来获取用户的访问令牌。然后,我们可以使用访问令牌来获取用户的谷歌账号信息。 在 PHP 中,我们可以使用谷歌客户端库提供的方法来完成上述步骤。首先,我们需要创建一个谷歌客户端对象,并设置我们在开发者控制台中获取到的客户端ID和客户端密钥。 然后,我们可以生成一个谷歌授权链接,并将用户重定向到该链接。当用户成功登录并授权后,我们可以使用谷歌客户端的 `fetchAccessTokenWithAuthCode` 方法来获取用户的访问令牌。 一旦我们获取到访问令牌,我们可以使用谷歌客户端的 `get` 方法来获取用户的谷歌账号信息,例如用户的姓名、电子邮件等。 最后,我们可以根据用户的谷歌账号信息进行相应的处理,例如创建新用户账号,或将其与现有用户账号进行关联。 通过这样的方式,我们就可以在 PHP 中实现谷歌第三方登录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值