本文整理汇总了Java中com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest类的典型用法代码示例。如果您正苦于以下问题:Java GoogleAuthorizationCodeTokenRequest类的具体用法?Java GoogleAuthorizationCodeTokenRequest怎么用?Java GoogleAuthorizationCodeTokenRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GoogleAuthorizationCodeTokenRequest类属于com.google.api.client.googleapis.auth.oauth2包,在下文中一共展示了GoogleAuthorizationCodeTokenRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
private static void authorize(DataStoreFactory storeFactory, String userId) throws Exception {
// Depending on your application, there may be more appropriate ways of
// performing the authorization flow (such as on a servlet), see
// https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#authorization_code_flow
// for more information.
GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(),
new JacksonFactory(),
CLIENT_ID,
CLIENT_SECRET,
Arrays.asList(SCOPE))
.setDataStoreFactory(storeFactory)
// Set the access type to offline so that the token can be refreshed.
// By default, the library will automatically refresh tokens when it
// can, but this can be turned off by setting
// api.dfp.refreshOAuth2Token=false in your ads.properties file.
.setAccessType("offline").build();
String authorizeUrl =
authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);
// Wait for the authorization code.
System.out.println("Type the code you received here: ");
@SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();
// Authorize the OAuth2 token.
GoogleAuthorizationCodeTokenRequest tokenRequest =
authorizationFlow.newTokenRequest(authorizationCode);
tokenRequest.setRedirectUri(CALLBACK_URL);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
// Store the credential for the user.
authorizationFlow.createAndStoreCredential(tokenResponse, userId);
}
示例2: getCredentials
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
/**
* Retrieve OAuth 2.0 credentials.
*
* @return OAuth 2.0 Credential instance.
* @throws IOException
*/
private Credential getCredentials() throws IOException {
String code = tokenField.getText();
HttpTransport transport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String CLIENT_SECRET = "EPME5fbwiNLCcMsnj3jVoXeY";
// Step 2: Exchange -->
GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(
transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, code,
REDIRECT_URI).execute();
// End of Step 2 <--
// Build a new GoogleCredential instance and return it.
return new GoogleCredential.Builder()
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.setJsonFactory(jsonFactory).setTransport(transport).build()
.setAccessToken(response.getAccessToken())
.setRefreshToken(response.getRefreshToken());
}
示例3: exchangeCode
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
protected Credential exchangeCode(
String authorizationCode, String redirectUri)
throws CodeExchangeException {
try {
GoogleAuthorizationCodeFlow flow = getFlow();
GoogleAuthorizationCodeTokenRequest token = flow.newTokenRequest(
authorizationCode);
token.setRedirectUri(redirectUri);
GoogleTokenResponse response = token.execute();
return flow.createAndStoreCredential(response, null);
}
catch (IOException e) {
System.err.println("An error occurred: " + e);
throw new CodeExchangeException();
}
}
示例4: handleTokens
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
protected void handleTokens(Map<String, String> params) {
final GoogleClientSecrets secrets = new GoogleClientSecrets().setInstalled(
new GoogleClientSecrets.Details().
setClientId(params.get(constants.getApplicationClientId())).
setClientSecret(params.get(constants.getApplicationClientSecret()))
);
try {
final GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
this.httpTransport,
this.jsonFactory,
secrets.getDetails().getClientId(),
secrets.getDetails().getClientSecret(),
params.get(constants.getApplicationOauthCode()),
constants.getRedirectUri()
).execute();
params.put(constants.getApplicationRefreshToken(), tokenResponse.getRefreshToken());
params.put(constants.getApplicationAccessToken(), tokenResponse.getAccessToken());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例5: retrieve
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
/**
* Retrieves a new access token by exchanging the given code with OAuth2
* end-points.
* @param code Exchange code.
* @return A credential object.
*/
public Credential retrieve(String code) {
try {
GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(
transport,
jsonFactory,
clientSecrets.getWeb().getClientId(),
clientSecrets.getWeb().getClientSecret(),
code,
clientSecrets.getWeb().getRedirectUris().get(0)).execute();
return buildEmpty().setAccessToken(response.getAccessToken());
} catch (IOException e) {
new RuntimeException("An unknown problem occured while retrieving token");
}
return null;
}
示例6: run
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
public void run() {
try {
mAuthResp = new GoogleAuthorizationCodeTokenRequest(transport,
jsonFactory,
CLIENT_ID,
CLIENT_SECRET,
mReturnedWebCode, REDIRECT_URI).execute();
String userEmail = getUserEmail(mAuthResp.getAccessToken());
if (null != userEmail) {
mAccessResult = RESULT_OK;
mAccessToken = userEmail;
} else { // failed login
mAccessResult = RESULT_CANCELED;
}
} catch (IOException ioe) {
Timber.e("Authentication Error: " + ioe.getMessage());
}
finish();
}
示例7: setUp
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
public static People setUp(Context context, String serverAuthCode) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Redirect URL for web based applications.
// Can be empty too.
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
// Exchange auth code for access token
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
httpTransport,
jsonFactory,
context.getString(R.string.clientID),
context.getString(R.string.clientSecret),
serverAuthCode,
redirectUrl).execute();
// Then, create a GoogleCredential object using the tokens from GoogleTokenResponse
GoogleCredential credential = new GoogleCredential.Builder()
.setClientSecrets(context.getString(R.string.clientID), context.getString(R.string.clientSecret))
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.build();
credential.setFromTokenResponse(tokenResponse);
// credential can then be used to access Google services
return new People.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
示例8: exchangeCode
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
/**
* @param code
* @return
* @throws IOException
*/
public boolean exchangeCode(String code) {
this.authorizationCode = code;
// Step 2: Exchange --> exchange code for tokens
boolean result = false;
String callbackUri = clientSecrets.getDetails().getRedirectUris().get(0);
GoogleTokenResponse response;
try {
response = new GoogleAuthorizationCodeTokenRequest(Global.HTTP_TRANSPORT, Global.JSON_FACTORY,
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(), code, callbackUri).execute();
// Build a new GoogleCredential instance and return it.
credential = new GoogleCredential.Builder()
.setClientSecrets(clientSecrets)
.setJsonFactory(Global.JSON_FACTORY)
.setTransport(Global.HTTP_TRANSPORT)
.build()
.setAccessToken(response.getAccessToken())
.setRefreshToken(response.getRefreshToken());
result = true;
} catch (IOException e) {
e.printStackTrace();
}
// End of Step 2 <--
return result;
}
示例9: getCredentialFromOneTimeCode
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的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;
}
示例10: exchangeCode
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
/**
* Exchanges the `code` member of the given AccessToken object, and returns
* the relevant GoogleTokenResponse.
* @param accessToken Container of authorization code to exchange.
* @return Token response from Google indicating token information.
* @throws TokenDataException Failed to exchange code (code invalid).
*/
private GoogleTokenResponse exchangeCode(TokenData accessToken)
throws TokenDataException {
try {
// Upgrade the authorization code into an access and refresh token.
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, accessToken.code,
"postmessage").execute();
return tokenResponse;
} catch (IOException e) {
throw new TokenDataException(e.getMessage());
}
}
示例11: getOAuth2Credential
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
throws Exception {
GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(),
new JacksonFactory(),
clientSecrets,
SCOPES)
// Set the access type to offline so that the token can be refreshed.
// By default, the library will automatically refresh tokens when it
// can, but this can be turned off by setting
// api.dfp.refreshOAuth2Token=false in your ads.properties file.
.setAccessType("offline").build();
String authorizeUrl =
authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);
// Wait for the authorization code.
System.out.println("Type the code you received here: ");
@SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();
// Authorize the OAuth2 token.
GoogleAuthorizationCodeTokenRequest tokenRequest =
authorizationFlow.newTokenRequest(authorizationCode);
tokenRequest.setRedirectUri(CALLBACK_URL);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
// Create the OAuth2 credential.
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets(clientSecrets)
.build();
// Set authorized credentials.
credential.setFromTokenResponse(tokenResponse);
return credential;
}
示例12: getOAuth2Credential
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
throws Exception {
GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(),
new JacksonFactory(),
clientSecrets,
Arrays.asList(SCOPE))
// Set the access type to offline so that the token can be refreshed.
// By default, the library will automatically refresh tokens when it
// can, but this can be turned off by setting
// api.dfp.refreshOAuth2Token=false in your ads.properties file.
.setAccessType("offline").build();
String authorizeUrl =
authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);
// Wait for the authorization code.
System.out.println("Type the code you received here: ");
@SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();
// Authorize the OAuth2 token.
GoogleAuthorizationCodeTokenRequest tokenRequest =
authorizationFlow.newTokenRequest(authorizationCode);
tokenRequest.setRedirectUri(CALLBACK_URL);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
// Create the OAuth2 credential.
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets(clientSecrets)
.build();
// Set authorized credentials.
credential.setFromTokenResponse(tokenResponse);
return credential;
}
示例13: getOAuth2Credential
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
throws Exception {
GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(),
new JacksonFactory(),
clientSecrets,
SCOPES)
// Set the access type to offline so that the token can be refreshed.
// By default, the library will automatically refresh tokens when it
// can, but this can be turned off by setting
// api.adwords.refreshOAuth2Token=false in your ads.properties file.
.setAccessType("offline").build();
String authorizeUrl =
authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);
// Wait for the authorization code.
System.out.println("Type the code you received here: ");
@SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();
// Authorize the OAuth2 token.
GoogleAuthorizationCodeTokenRequest tokenRequest =
authorizationFlow.newTokenRequest(authorizationCode);
tokenRequest.setRedirectUri(CALLBACK_URL);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
// Create the OAuth2 credential.
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets(clientSecrets)
.build();
// Set authorized credentials.
credential.setFromTokenResponse(tokenResponse);
return credential;
}
示例14: getOAuth2Credential
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
throws Exception {
GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(),
new JacksonFactory(),
clientSecrets,
Arrays.asList(SCOPE))
// Set the access type to offline so that the token can be refreshed.
// By default, the library will automatically refresh tokens when it
// can, but this can be turned off by setting
// api.adwords.refreshOAuth2Token=false in your ads.properties file.
.setAccessType("offline").build();
String authorizeUrl =
authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);
// Wait for the authorization code.
System.out.println("Type the code you received here: ");
@SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();
// Authorize the OAuth2 token.
GoogleAuthorizationCodeTokenRequest tokenRequest =
authorizationFlow.newTokenRequest(authorizationCode);
tokenRequest.setRedirectUri(CALLBACK_URL);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
// Create the OAuth2 credential.
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets(clientSecrets)
.build();
// Set authorized credentials.
credential.setFromTokenResponse(tokenResponse);
return credential;
}
示例15: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入依赖的package包/类
private static void authorize(DataStoreFactory storeFactory, String userId) throws Exception {
// Depending on your application, there may be more appropriate ways of
// performing the authorization flow (such as on a servlet), see
// https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#authorization_code_flow
// for more information.
GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(),
new JacksonFactory(),
CLIENT_ID,
CLIENT_SECRET,
Arrays.asList(SCOPE))
.setDataStoreFactory(storeFactory)
// Set the access type to offline so that the token can be refreshed.
// By default, the library will automatically refresh tokens when it
// can, but this can be turned off by setting
// api.adwords.refreshOAuth2Token=false in your ads.properties file.
.setAccessType("offline").build();
String authorizeUrl =
authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);
// Wait for the authorization code.
System.out.println("Type the code you received here: ");
@SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();
// Authorize the OAuth2 token.
GoogleAuthorizationCodeTokenRequest tokenRequest =
authorizationFlow.newTokenRequest(authorizationCode);
tokenRequest.setRedirectUri(CALLBACK_URL);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
// Store the credential for the user.
authorizationFlow.createAndStoreCredential(tokenResponse, userId);
}