Google登录SDK

一、接入的准备工作

官方文档链接地址:开始使用一键登录和注册

按照步骤进行接入即可

二、项目参考(Unity项目)

注意:代码版本如果不适用新的Google API 请自行参考最新版本接口

SDKGoogleSignInActivity 主要用于登录的代码。UnityPlayer.UnitySendMessage的参数按照自己项目填写,com.xxxxx.my也是填写自己项目的路径。

package com.xxxxx.my;import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;import com.zhijingweilai.domino.R;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.unity3d.player.UnityPlayer;/*** Activity to demonstrate basic retrieval of the Google user's ID, email address, and basic* profile.*/
public class SDKGoogleSignInActivity extends AppCompatActivity {private static final String TAG = "SignInActivity";private static final int RC_SIGN_IN = 1234; // Can be any integer unique to the Activity.private GoogleSignInClient mGoogleSignInClient;private TextView mStatusTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// [START configure_signin]// Configure sign-in to request the user's ID, email address, and basic// profile. ID and basic profile are included in DEFAULT_SIGN_IN.String clientId = getString(R.string.server_client_id);GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().requestIdToken(clientId).build();// [END configure_signin]// [START build_client]// Build a GoogleSignInClient with the options specified by gso.mGoogleSignInClient = GoogleSignIn.getClient(this, gso);// [END build_client]}public void SendLogin() {GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);if (account != null && !account.isExpired()) {updateUI(account, "");}elsesignIn();}// [START onActivityResult]@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);if (requestCode == RC_SIGN_IN) {// The Task returned from this call is always completed, no need to attach// a listener.Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);handleSignInResult(task);}}// [END onActivityResult]// [START handleSignInResult]private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {try {GoogleSignInAccount account = completedTask.getResult(ApiException.class);// Signed in successfully, show authenticated UI.updateUI(account, "");} catch (ApiException e) {// The ApiException status code indicates the detailed failure reason.// Please refer to the GoogleSignInStatusCodes class reference for more information.Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());updateUI(null, e.toString());}}// [END handleSignInResult]// [START signIn]public void signIn() {Intent signInIntent = mGoogleSignInClient.getSignInIntent();startActivityForResult(signInIntent, RC_SIGN_IN);}// [END signIn]// [START signOut]public void signOut() {mGoogleSignInClient.signOut().addOnCompleteListener(this, new OnCompleteListener<Void>() {@Overridepublic void onComplete(@NonNull Task<Void> task) {String str = new JsonToString().AddJSONObject("result", 0).AddJSONObject("msg", "signout").GetString();Log.i("Google", "Google SignOut str=" + str);UnityPlayer.UnitySendMessage("SDK_callback", "OnGGSignOutResult", str);finish();}});}// [END signOut]// [START revokeAccess]public void revokeAccess() {mGoogleSignInClient.revokeAccess().addOnCompleteListener(this, new OnCompleteListener<Void>() {@Overridepublic void onComplete(@NonNull Task<Void> task) {String str = new JsonToString().AddJSONObject("result", 0).AddJSONObject("msg", "revokeAccess").GetString();Log.i("Google", "Google RevokeAccess str=" + str);UnityPlayer.UnitySendMessage("SDK_callback", "OnGGRevokeAccessResult", str);finish();}});}// [END revokeAccess]private void updateUI(@Nullable GoogleSignInAccount acct, String err) {if (acct != null){String idToken = acct.getIdToken();String personName = acct.getDisplayName();String personGivenName = acct.getGivenName();String personFamilyName = acct.getFamilyName();String personEmail = acct.getEmail();String personId = acct.getId();Uri personPhoto = acct.getPhotoUrl();Log.i("Google", "Google SignIn URI=" + personPhoto.toString());Log.i("Google", "Google SignIn URI=" + personPhoto.getPath());Log.i("Google", "Google SignIn URI=" + personPhoto.getQuery());String str = new JsonToString().AddJSONObject("result", 0).AddJSONObject("msg", "signin").AddJSONObject("refresh_token", idToken).AddJSONObject("personName", personName).AddJSONObject("personGivenName", personGivenName).AddJSONObject("personFamilyName", personFamilyName).AddJSONObject("personEmail", personEmail).AddJSONObject("loginId", personId).AddJSONObject("iconUrl", personPhoto.toString()).GetString();Log.i("Google", "Google SignIn str=" + str);UnityPlayer.UnitySendMessage("SDK_callback", "OnGGSignInResult", str);acct = null;}else{String str = new JsonToString().AddJSONObject("result", 1).AddJSONObject("msg", "signin").AddJSONObject("err", err).GetString();Log.i("Google", "Google SignIn str=" + str);UnityPlayer.UnitySendMessage("SDK_callback", "OnGGSignInResult", str);}finish();}}

 JsonToString 用于处理数据,将json数据转成string方便传输

package com.xxxxx.my;import org.json.JSONObject;public class JsonToString {private JSONObject jsonObj;public JsonToString(){jsonObj = new JSONObject();};public JsonToString AddJSONObject(String key, Object val) {try {jsonObj.put(key, val);}catch(Exception e) {}return this;}public String GetString(){String str = jsonObj.toString();return str;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/90811.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

设计模式备忘录+命令模式实现Word撤销恢复操作

文章目录 前言思路代码实现uml类图总结 前言 最近学习设计模式行为型的模式&#xff0c;学到了备忘录模式提到这个模式可以记录一个对象的状态属性值&#xff0c;用于下次复用&#xff0c;于是便想到了我们在Windows系统上使用的撤销操作&#xff0c;于是便想着使用这个模式进…

DCL介绍

文章目录 1. 简介2. DCL管理用户3. DCL权限控制 1. 简介 DCL英文全称为&#xff08;数据控制语言&#xff09;&#xff0c;用来管理数据库用户、控制数据库的访问权限 2. DCL管理用户 查询用户 Use mysql; select * from user;创建用户 create user 用户名主机名 Identifi…

【UE 材质】模型部分透明

材质节点如下&#xff0c;这里简单解释一下。首先通过“Mask”节点将"Texture Coordinate" 节点中的“G”通道分离出来&#xff0c;然后通过“if”节点进行判断&#xff0c;当值小于0.5时为透明&#xff0c;当颜色不小于5时为不透明。可以通过一个参数来控制模型透明…

C++实现客户端/服务端通信(一)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 C实现客户端/服务端通信&#xff08;一&#xff09; 网络编程的基本概念1. 客户端/服务器通信模型&#xff1a;2. socket函数&#xff1a;3. 主机字节序和网络字节序&#xf…

k8s 常用命令(四)

12、删除pod中的nginx服务及service [rootmaster ~]# kubectl delete deployment nginx -n kube-public [rootmaster ~]# kubectl delete svc -n kube-public nginx-service 13、查看endpoint的信息 [rootmaster ~]# kubectl get endpoints 14、修改/更新&#xff08;镜像、…

用Cmake build OpenCV后,在VS中查看OpenCV源码的方法(环境VS2022+openCV4.8.0) Part III

用Cmake build OpenCV后&#xff0c;在VS中查看OpenCV源码的方法(环境VS2022openCV4.8.0) Part III 用Cmake build OpenCV后&#xff0c;在VS中查看OpenCV源码的方法&#xff08;环境VS2022openCV4.8.0&#xff09; Part I_松下J27的博客-CSDN博客 用Cmake build OpenCV后&…

从零构建深度学习推理框架-11 Resnet

op和layer结构 在runtime_ir.cpp中&#xff0c;我们上一节只构建了input和output&#xff0c;对于中间layer的具体实现一直没有完成&#xff1a; for (const auto& kOperator : this->operators_) {if (kOperator->type "pnnx.Input") {this->input_o…

十五、模板方法模式

一、什么是模板方法模式 模板方法&#xff08;Template Method&#xff09;模式的定义如下&#xff1a;定义一个操作中的算法骨架&#xff0c;而将算法的一些步骤延迟到子类中&#xff0c;使得子类可以不改变该算法结构的情况下重定义该算法的某些特定步骤。 模板方法模式包含以…

zookeeper 3.8.1安装和入门使用

1、zookeeper环境搭建&#xff08;Windows单机版&#xff09; 1.1、 前提 必须安装jdk 1.8&#xff0c;配置jdk环境变量&#xff0c;步骤略 1.2、安装zookeeper 地址&#xff1a;https://zookeeper.apache.org/ 1.2.1、选择releases版本 1.2.2、下载安装包并解压 1.2.3、配…

成集云 | 钉钉财务费用单同步至畅捷通 | 解决方案

源系统成集云目标系统 方案介绍 财务管理作为企业管理中重要的组成部分&#xff0c;在企业的发展和成长中扮演着重要角色&#xff0c;成集云以钉钉费用单OA审批与畅捷通TCloud系统为例&#xff0c;与钉钉连接器深度融合&#xff0c;通过数据处理和字段匹配实现了费用…

【redis问题】Caused by: io.netty.channel

遇到的问题&#xff1a; 在使用 RedisTemplate 连接 Redis 进行操作的时候&#xff0c;发生了如下报错&#xff1a; 测试代码为&#xff1a; 配置文件&#xff1a; 问题根源&#xff1a; redis没有添加端口映射解决方案&#xff1a; 删除原来的redis容器&#xff0c;添加新…

高忆管理:大盘是什么意思?

随着市场经济的开展&#xff0c;股票、基金等金融出资产品的普及&#xff0c;大盘这个词汇也逐步进入了咱们的生活中。那么&#xff0c;大盘又是什么意思呢&#xff1f; 从基本概念视点来看&#xff0c;大盘是指股票市场全体走势的指标。出资者经过调查大盘指数的改变&#xf…