谷歌锁区号/谷歌邮箱老号-购买商城
谷歌play地区代改
Google Voice号码支持自助购买
谷歌锁区号购买商城]
美区VISA卡代开-可以用于aws,azure,FB,谷歌,亚马逊,速卖通,eBay,独立站,paypal等支付
如果您还有其他问题可以加我电报交流。
电报号:telegram:@tianmeiapp
本文包含一个完整的登录授权演示,从创建 API 到使用 Java 代码完成登录。
目录
了解 Auth2.0
相关文件:
Auth2.0 协议用于账户授权和登录。
举个例子来理解Auth2.0的设计模式。
周五下午你在公司点了一杯奶茶。楼下有个门禁,需要密码才能进入。当然,您不会告诉送货员密码。您告诉送货员在访问控制应用程序中注册访客登记卡。在您同意一系列访客管理指令后,通过短信向他发送验证码,小哥输入验证码获取访客卡即可顺利进入;
送货员进来后,拿这张门禁卡刷电梯。有两部电梯可以上到你的楼层。A电梯可以到公司办公室,B电梯可以到餐厅,但是小弟只能使用B电梯,因为你在餐厅等着,不用去办公室打扰其他同事;
周六,你在公司同一家店点了一杯奶茶。送奶茶的时候,发现自己申请的访客卡是临时的。您需要重新注册,但发送验证码时不再需要经过一系列访客管理。注意,因为门禁APP已经有你对快递小哥的授权记录;
点了两次,觉得这家店的奶茶很好吃,于是就在这家店开了会员。但有时开会时不能及时给外卖小哥访客卡验证码,于是要求店家注册会员卡,有了这张会员卡,就可以不经授权直接拿到访客卡;
有一天你上网看到一张拓海的近照,决定戒掉母乳,于是在门禁APP上取消了店家的访客卡和会员卡。
创建 API 服务
了解auth2.0后怎么样注册谷歌账号,开始授权登录正式使用。
现在演示的是一个测试项目,测试项目没有强验证,所以下面的信息可以随意填写。在确认可行性后创建生产项目时需要进行认证。
此外,您可能需要向审核员上传屏幕录制视频,以展示您使用 API 完成的工作。演示视频项目的内容需要是英文的。如果您的项目不是英文的,您可以在演示期间使用它。翻译成英语
向 帐户注册 API
用谷歌账号登录后创建[test]项目
创建项目【测试】后,来到控制台
题外话:如果你想使用一些功能,可以将Api添加到Api库中;例如,谷歌翻译、搜索和启用。(ps:翻译api收费,需要结算管理账号)
配置同意屏幕
同意屏幕:用户登录您的应用时向用户显示的信息,包括网站域名和徽标、隐私政策
应用领域
包含信息、网站域名、网站首页、隐私政策和服务条款四部分。
范围
即授权域。这也显示在用户同意屏幕上。
我们需要保存邮箱账号,所以只使用/auth//email的授权范围
创建凭据
选择 OAuth 客户端 ID
选择web应用并配置授权登录后的url(url需要是https协议,虽然只是一个测试项目)
创建后,获取客户端id和秘钥
使用 API 服务
导入maven
com.google.auth
google-auth-library-oauth2-http
0.22.2
配置类
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.zz.saas.shoplus.portal.bean.GoogleAuthorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* google登录授权配置
*/@Configuration
@Slf4j
public class GoogleAuthorizationConfig {
private static final String clientId = "your client id";
private static final String clientSecret = "your client secret";
private static final String applicationName = "your application name";
private static final String redirectUrl = "https://test.com/google-login";
@Bean(name = "googleAuthorization")
public GoogleAuthorization googleFeed() {
GoogleClientSecrets clientSecrets = null;
try {
GoogleClientSecrets.Details details = new GoogleClientSecrets.Details();
details.setClientId(clientId);
details.setClientSecret(clientSecret);
clientSecrets = new GoogleClientSecrets(); clientSecrets.setInstalled(details); } catch (Exception e) { log.error("authorization configuration error:{}", e.getMessage()); } // 构建bean return GoogleAuthorization.builder() .googleClientSecrets(clientSecrets) .applicationName(applicationName) .redirectUrl(redirectUrl) .build(); } }
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Collections;
import java.util.List;
/**
* google授权
*/@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class GoogleAuthorization {
// 应用名
private String applicationName;
// 重定向路径
private String redirectUrl;
// 应用凭证
private GoogleClientSecrets googleClientSecrets; // 授权域 private final static List scopes = Collections.singletonList( "https://www.googleapis.com/auth/userinfo.email" ); public List getScopes(){ return scopes; } }
获取链接以发起登录请求
// 导包信息
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.*;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
@Autowired
private GoogleAuthorizationService googleAuthorizationService;
public String authorizingUrl() throws GeneralSecurityException, IOException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// 创建验证流程对象
GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow
.Builder(httpTransport, jsonFactory, googleAuthorization.getGoogleClientSecrets(), googleAuthorization.getScopes())
// AccessType为离线offline,才能获得Refresh Token
.setAccessType("offline").build();
if (googleAuthorizationCodeFlow != null) {
// 返回跳转登录请求
return googleAuthorizationCodeFlow.newAuthorizationUrl().setRedirectUri(googleAuthorization.getRedirectUrl()).build();
}
return null;
}
调用方法获取重定向的url:
:///-login&=code&scope=
我们来看看参数
在浏览器中访问这个url,跳转到同意界面,可以看到之前创建的api的应用信息
登录谷歌账号,授权后462,因为域名随便填怎么样注册谷歌账号,但主要是获取重定向的URL
重定向网址中填写了一个授权码,然后就可以使用这个授权码进行授权了
使用授权码授权
获取带有授权码的登录令牌
// 导包信息
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.*;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
public void authorizing(String authorizationCode) throws GeneralSecurityException, IOException {
// 创建请求凭证
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow
.Builder(httpTransport, jsonFactory, googleAuthorization.getGoogleClientSecrets(), googleAuthorization.getScopes())
// AccessType为离线offline,才能获得Refresh Token
.setAccessType("offline").build();
GoogleAuthorizationCodeTokenRequest tokenRequest = googleAuthorizationCodeFlow.newTokenRequest(authorizationCode);
tokenRequest.setRedirectUri(googleAuthorization.getRedirectUrl());
// 发起授权请求,获得Token和Refresh Token
GoogleTokenResponse tokenResponse = tokenRequest.execute();
String token = tokenResponse.getAccessToken();
String refreshToken = tokenResponse.getRefreshToken();
// 获得email
String email = null;
if (StringUtils.isNotBlank(tokenResponse.getIdToken())) {
GoogleIdTokenVerifier idTokenVerifier = new GoogleIdTokenVerifier.Builder(googleAuthorizationCodeFlow.getTransport(), googleAuthorizationCodeFlow.getJsonFactory()).build();
idTokenVerifier.verify(tokenResponse.getIdToken());
GoogleIdToken googleIdToken = idTokenVerifier.verify(tokenResponse.getIdToken());
if (googleIdToken != null && googleIdToken.getPayload() != null) {
email = googleIdToken.getPayload().getEmail();
}
}
// todo 保留账号token、refreshToken、email信息
}
令牌
令牌的有效期为 1 小时。令牌过期后,用户可以跳转到登录页面重新登录;
但是如果需要离线使用谷歌api,可以使用Token
public String refreshToken(String refreshToken) throws IOException {
String token = null;
// 创建刷新请求对象
GoogleRefreshTokenRequest googleRefreshTokenRequest = new GoogleRefreshTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
refreshToken,
googleAuthorization.getGoogleClientSecrets().getDetails().getClientId(),
googleAuthorization.getGoogleClientSecrets().getDetails().getClientSecret());
// 发起刷新请求
GoogleTokenResponse googleTokenResponse = googleRefreshTokenRequest.execute();
if (googleTokenResponse != null && StringUtils.isNotBlank(googleTokenResponse.getAccessToken())) {
token = googleTokenResponse.getAccessToken();
}
return null;
}
谷歌锁区号/谷歌邮箱老号-购买商城
谷歌play地区代改
Google Voice号码支持自助购买
谷歌锁区号购买商城]
美区VISA卡代开-可以用于aws,azure,FB,谷歌,亚马逊,速卖通,eBay,独立站,paypal等支付
如果您还有其他问题可以加我电报交流。
电报号:telegram:@tianmeiapp
原文链接:确认可行性后创建生产项目时需要进行认证吗?(图),转载请注明来源!