• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java Tokeninfo类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.google.api.services.oauth2.model.Tokeninfo的典型用法代码示例。如果您正苦于以下问题:Java Tokeninfo类的具体用法?Java Tokeninfo怎么用?Java Tokeninfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Tokeninfo类属于com.google.api.services.oauth2.model包,在下文中一共展示了Tokeninfo类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: getCredentialFromOneTimeCode

import com.google.api.services.oauth2.model.Tokeninfo; //导入依赖的package包/类
public static GoogleCredential getCredentialFromOneTimeCode(String userEmail,String code) throws IOException, OAuthRequestException {
	
    // https://developers.google.com/+/web/signin/server-side-flow

	HttpTransport httpTransport = GoogleServices.getInstance().httpTransport;
	JsonFactory jsonFactory = GoogleServices.getInstance().jsonFactory;
	String clientId = Config.get(Config.WEB_CLIENT_ID);
	String clientSecret = Config.get(Config.WEB_CLIENT_SECRET);
	
    // Upgrade the authorization code into an access and refresh token.
    GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(httpTransport, jsonFactory, clientId, clientSecret, code, "postmessage").execute();
   
    // Create a credential representation of the token data.
    GoogleCredential credential = GoogleServices.getCredentialBuilder().build().setFromTokenResponse(tokenResponse);

    // Check that the token is valid.
    Oauth2 oauth2 = new Oauth2.Builder(httpTransport, jsonFactory, credential).setApplicationName(Config.get(Config.APPLICATION_NAME)).build();
    Tokeninfo tokenInfo = oauth2.tokeninfo().setAccessToken(credential.getAccessToken()).execute();
    
    // If there was an error in the token info, abort.
    if (tokenInfo.containsKey("error")) throw new OAuthRequestException(tokenInfo.get("error").toString());

    // Make sure the token we got is for the intended user.
    if (SystemProperty.environment.value()!=SystemProperty.Environment.Value.Development && tokenInfo.getUserId().equals(userEmail)) throw new OAuthRequestException("Token's user ID doesn't match given user ID.");
    
    // Make sure the token we got is for our app.
    if (!tokenInfo.getIssuedTo().equals(clientId)) throw new OAuthRequestException("Token's client ID does not match app's.");
    
    return credential;
}
 
开发者ID:andryfailli,项目名称:teampot,代码行数:31,代码来源:GoogleServices.java


示例2: verifyToken

import com.google.api.services.oauth2.model.Tokeninfo; //导入依赖的package包/类
/**
  * Verify that the token in the given credential is valid.
  * @param credential Credential to verify.
  * @return Google user ID for which token was issued.
  * @throws TokenVerificationException Credential is not valid.
  * @throws IOException Could not verify Credential because of a network
  *                     failure.
  */
 private String verifyToken(GoogleCredential credential)
     throws TokenVerificationException, IOException {
   // Check that the token is valid.
   Oauth2 oauth2 = new Oauth2.Builder(TRANSPORT, JSON_FACTORY, credential)
       .build();
   Tokeninfo tokenInfo = oauth2.tokeninfo()
       .setAccessToken(credential.getAccessToken()).execute();
   // If there was an error in the token info, abort.
   if (tokenInfo.containsKey("error")) {
     throw new TokenVerificationException(tokenInfo.get("error").toString());
   }
   
   if (credential.getExpiresInSeconds() == null) {
   	// Set the expiry time if it hasn't already been set.
   	int expiresIn = tokenInfo.getExpiresIn();
	credential.setExpiresInSeconds((long) expiresIn);
	credential.setExpirationTimeMilliseconds(System.currentTimeMillis() + expiresIn * 1000);
   }
   
   Pattern p = Pattern.compile("(\\d+)([-]?)(.*)$");
Matcher issuedTo = p.matcher(CLIENT_ID);
Matcher localId = p.matcher(tokenInfo.getIssuedTo());

   // Make sure the token we got is for our app.
   if (!issuedTo.matches() || !localId.matches() 
   		|| !issuedTo.group(1).equals(localId.group(1))) {

     throw new TokenVerificationException(
         "Token's client ID does not match app's.");
   }
   
   return tokenInfo.getUserId();
 }
 
开发者ID:DesenvolvedoresGoogle,项目名称:Allow,代码行数:42,代码来源:ConnectServlet.java



注:本文中的com.google.api.services.oauth2.model.Tokeninfo类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java ErrorInfo类代码示例发布时间:2022-05-22
下一篇:
Java SwaRefAdapterMarker类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap