本文整理匯總了Java中com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow類的典型用法代碼示例。如果您正苦於以下問題:Java GoogleAuthorizationCodeFlow類的具體用法?Java GoogleAuthorizationCodeFlow怎麽用?Java GoogleAuthorizationCodeFlow使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
GoogleAuthorizationCodeFlow類屬於com.google.api.client.googleapis.auth.oauth2包,在下文中一共展示了GoogleAuthorizationCodeFlow類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
* Authorizes the installed application to access user's protected data.
*
* @param scopes list of scopes needed to run youtube upload.
* @param clientSecret the client secret from Google API console
* @param credentialDatastore name of the credential datastore to cache OAuth tokens
*/
public static Credential authorize(
Collection<String> scopes, String clientSecret, String credentialDatastore)
throws IOException {
// Load client secrets
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new StringReader(clientSecret));
// This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
FileDataStoreFactory fileDataStoreFactory =
new FileDataStoreFactory(new File(getCredentialsDirectory()));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes)
.setCredentialDataStore(datastore)
.build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
示例2: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
* Creates an authorized Credential object.
* @return an authorized Credential object.
* @throws IOException
*/
public static void authorize() throws IOException {
// Load client secrets.
InputStream in = Authorization.class.getClass().getResourceAsStream("/gallery/configs/client_id.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize(NICK);
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
MAIN = credential;
}
示例3: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的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);
}
示例4: getCredential
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
public final Credential getCredential() {
if (googleCredential == null) {
try {
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
secureHttpTransport,
JacksonFactory.getDefaultInstance(),
googleSecrets,
requiredScopes)
.setAccessType("offline")
.setDataStoreFactory(new MemoryDataStoreFactory())
.build();
googleCredential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
.authorize("user");
} catch (IOException e) {
//Will not occur
logger.fatal(e);
throw new RuntimeException();
}
}
return googleCredential;
}
示例5: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
* Authorizes the installed application to access user's protected data.
*/
public static void authorize(String userID, boolean driveAPI) throws IOException {
// load client secrets
// set up authorization code flow
Collection<String> scopes = new ArrayList<String>();
scopes.add(GamesScopes.GAMES);
if (driveAPI)
scopes.add(DriveScopes.DRIVE_APPDATA);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
clientSecrets, scopes).setDataStoreFactory(dataStoreFactory).build();
// authorize
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) {
// Override open browser not working well on Linux and maybe other
// OS.
protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws java.io.IOException {
Gdx.net.openURI(authorizationUrl.build());
}
}.authorize(userID);
games = new Games.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(applicationName).build();
if (driveAPI)
drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(applicationName).build();
}
示例6: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
* Creates an authorized Credential object.
* @return an authorized Credential object.
* @throws Exception
*/
public static Credential authorize() throws Exception {
// Load client secrets.
InputStream in =
SpreadsheetUtils.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
示例7: buildCodeFlow
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
* Gets the credentials stored in GAE. If expired, uses the refresh token to
* get a new access token from OAuth
* @return
* @throws IOException
*/
// START:flow
public static AuthorizationCodeFlow buildCodeFlow()
throws IOException
{
return new GoogleAuthorizationCodeFlow.Builder(
new UrlFetchTransport(),
new JacksonFactory(),
WEB_CLIENT_ID,
WEB_CLIENT_SECRET,
SCOPES)
.setApprovalPrompt( "force" )
.setAccessType("offline")
.setCredentialDataStore( getDataStore() )
.build();
}
示例8: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
* Creates an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
Login.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
示例9: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
* Creates an authorized Credential object.
* @return an authorized Credential object.
* @throws ResourceServerException Incorrect authorization
*/
public static Credential authorize() {
// Load client secrets.
final InputStream in =
GoogleStorageServiceImpl.class.getResourceAsStream("/client_secret.json");
Credential credential;
try {
final GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
final GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
} catch (final IOException e) {
throw new ResourceServerException("Incorrect authorization", e);
}
return credential;
}
示例10: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
// load client secrets
Reader in = new FileReader("Local directory path for client_secrets.json file");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, in);
if (clientSecrets.getDetails().getClientId().startsWith("Your ")
|| clientSecrets.getDetails().getClientSecret().startsWith("Your ")) {
System.out.println(
"Enter Client ID and Secret from https://code.google.com/apis/console/?api=plus "
+ "into client_secrets.json");
System.exit(1);
}
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton(PlusScopes.PLUS_ME)).setDataStoreFactory(
dataStoreFactory).build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
示例11: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
* Creates a new authorized Credential object from a response token
* @param token (String)
* @param userId (String)
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize(String token, String userId) throws IOException
{
// Load client secrets.
InputStream in = new FileInputStream(Main.getBotSettingsManager().getGoogleOAuthSecret());
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = (new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES))
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
// remove any account previously associated with the token
flow.getCredentialDataStore().delete(userId);
// create the new credential
GoogleTokenResponse response = flow.newTokenRequest(token)
.setRedirectUri(clientSecrets.getDetails().getRedirectUris().get(0)).execute();
return flow.createAndStoreCredential(response, userId);
}
示例12: newAuthorizationUrl
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/**
*
* @return
* @throws IOException
*/
public static String newAuthorizationUrl() throws IOException
{
// Load client secrets.
InputStream in = new FileInputStream(Main.getBotSettingsManager().getGoogleOAuthSecret());
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = (new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES))
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
return flow.newAuthorizationUrl()
.setScopes(SCOPES)
.setAccessType("offline")
.setClientId(clientSecrets.getDetails().getClientId())
.setRedirectUri(clientSecrets.getDetails().getRedirectUris().get(0))
.toString();
}
示例13: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
/***
* Crates a Credential object authorized by the Google API.
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException{
InputStream in = CalendarAPI.class.getResourceAsStream("resources/client_secret.json");
if(in == null) throw new IOException("Client information not found.");
GoogleClientSecrets secrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
http, jsonFactory, secrets, scopes)
.setDataStoreFactory(dataFactory)
.setAccessType("offline")
.build();
Credential cred = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
return cred;
}
示例14: CachingGoogleAuthCodeFlow
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
private CachingGoogleAuthCodeFlow(final int authCacheTtl,
final String clientId,
final String clientSecret,
final String organizationId,
final String redirectUri) throws IOException {
this.authCache = Caffeine.newBuilder()
.maximumSize(4096)
.expireAfterWrite(authCacheTtl, MILLISECONDS)
.build(k -> this.isOrganizationMember(k, true));
this.authFlow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT,
JSON_FACTORY,
clientId,
clientSecret,
SCOPES
).setDataStoreFactory(
DATA_STORE_FACTORY
).setAccessType(
"offline"
).setApprovalPrompt(
"force"
).build();
this.organizationId = organizationId;
this.redirectUri = redirectUri;
}
示例15: authorize
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入依賴的package包/類
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println(
"Enter Client ID and Secret from https://console.developers.google.com/project/_/apiui/credential "
+ "into src/main/resources/client_secrets.json");
System.exit(1);
}
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore)
.build();
LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}