本文整理汇总了Java中com.google.api.services.oauth2.model.Tokeninfo.containsKey方法的典型用法代码示例。如果您正苦于以下问题:Java Tokeninfo.containsKey方法的具体用法?Java Tokeninfo.containsKey怎么用?Java Tokeninfo.containsKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.api.services.oauth2.model.Tokeninfo
的用法示例。
在下文中一共展示了Tokeninfo.containsKey方法的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;
}
示例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();
}