# 开通

  • 注册Google账号
  • 登录Google Cloud Platform,打开“选择项目”界面选择已经创建的项目,如果没有创建项目,点击“新建项目”根据页面提示创建项目
  • Select "APIs and Services" -> "Credentials" in the left navigation bar to open the management page
  • Click "Create Credentials" -> "OAuth Client ID", select "Application Type" according to the prompts, and enter Android/iOS platform configuration information as required
  • The iOS platform can obtain the client ID after the credential is created (the Android platform does not need the client ID)

更多信息详见 申请开通Google登录操作指南

# 配置

  • iOS平台客户端ID Google云平台创建的OAuth2.0凭证的iOS平台客户端ID

Notice

  • The standard real machine running base in HBuilderX uses the AppID and other information that DCloud applies for the HBuilder application, which is only used to experience the Google login function
  • The configuration parameters need to be submitted to the cloud to be packaged to take effect. Please use the [custom debugging base] when the real machine is running (https://ask.dcloud.net.cn/article/35115)

# 使用Google登录

# 示例代码

  • uni-app项目
uni.login({
    provider: 'google',
    success: function (loginRes) {
        // login successful
        uni.getUserInfo({
            provider: 'google',
            success: function(info) {
                // Obtain user information successfully, info.authResult saves user information
            }
        })
    },
    fail: function (err) {
        // 登录授权失败
        // err.code is the error code
    }
});
  • 5+ App项目
var googleOauth = null;
plus.oauth.getServices(function(services) {
	for (var i in services) {
		var service = services[i];
		// 获取微信登录对象
		if (service.id == 'google') {
			googleOauth = service;
			break;
		}
	}
	googleOauth.login( function(oauth){
		// 授权成功,googleOauth.authResult 中保存授权信息
	}, function(err) {
    // 登录授权失败
    // err.code is the error code
	})
}, function(err) {
	// Failed to get services
})