当前位置: 首页>>代码示例>>Java>>正文


Java Tokeninfo类代码示例

本文整理汇总了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;未经允许,请勿转载。