GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER)) // The serverClientId is an OAuth 2.0 web client ID // Details at: https://developers.google.com/identity/sign-in/android/?utm_campaign=android_discussion_server_021116&utm_source=anddev&utm_medium=blogstart step 4 .requestServerAuthCode(serverClientId) .requestEmail() .build();
if (result.isSuccess()) { GoogleSignInAccount acct = result.getSignInAccount(); String authCode = acct.getServerAuthCode(); }
if
(result.isSuccess()) {
GoogleSignInAccount
acct = result.getSignInAccount();
String
authCode = acct.getServerAuthCode(); }
// Set path to the Web application client_secret_*.json file you downloaded from the // Google Developers Console: https://console.developers.google.com/project/_/apiui/credential // You can also find your Web application client ID and client secret from the // console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest // object. String CLIENT_SECRET_FILE = "/path/to/client_secret.json"; // Be careful not to share this! String REDIRECT_URI = "/path/to/web_app_redirect" // Can be empty if you don’t use web redirects // Exchange auth code for access token GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE)); GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest( new NetHttpTransport(), JacksonFactory.getDefaultInstance(), "https://www.googleapis.com/oauth2/v4/token", clientSecrets.getDetails().getClientId(), clientSecrets.getDetails().getClientSecret(), authCode, REDIRECT_URI) .execute(); String accessToken = tokenResponse.getAccessToken(); String refreshToken = tokenResponse.getRefreshToken(); Long expiresInSeconds = tokenResponse.getExpiresInSeconds(); // You can also get an ID token from the exchange result if basic profile scopes are requested // e.g. starting GoogleSignInOptions.Builder from GoogleSignInOptions.DEFAULT_SIGN_IN like the // sample code as used here: http://goo.gl/0Unpq8 // // GoogleIdToken googleIdToken = tokenResponse.parseIdToken();
// Set path to the Web application client_secret_*.json file you downloaded from the // Google Developers Console: https://console.developers.google.com/project/_/apiui/credential // You can also find your Web application client ID and client secret from the // console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest // object.
String CLIENT_SECRET_FILE =
"/path/to/client_secret.json"
; // Be careful not to share this! String REDIRECT_URI =
"/path/to/web_app_redirect"
// Can be empty if you don’t use web redirects
// Exchange auth code for access token
GoogleClientSecrets
clientSecrets =
.load(
JacksonFactory
.getDefaultInstance(),
new
FileReader
(CLIENT_SECRET_FILE));
GoogleTokenResponse
tokenResponse =
GoogleAuthorizationCodeTokenRequest
(
NetHttpTransport
(),
"https://www.googleapis.com/oauth2/v4/token"
, clientSecrets.getDetails().getClientId(), clientSecrets.getDetails().getClientSecret(), authCode, REDIRECT_URI) .execute(); String accessToken = tokenResponse.getAccessToken(); String refreshToken = tokenResponse.getRefreshToken(); Long expiresInSeconds = tokenResponse.getExpiresInSeconds();
// You can also get an ID token from the exchange result if basic profile scopes are requested // e.g. starting GoogleSignInOptions.Builder from GoogleSignInOptions.DEFAULT_SIGN_IN like the // sample code as used here: http://goo.gl/0Unpq8 // // GoogleIdToken googleIdToken = tokenResponse.parseIdToken();
GoogleCredential credential = new GoogleCredential.Builder() .setTransport(new NetHttpTransport()) .setJsonFactory(JacksonFactory.getDefaultInstance()) .setClientSecrets(clientSecrets) .build(); credential.setAccessToken(accessToken); credential.setExpiresInSeconds(expiresInSeconds); credential.setRefreshToken(refreshToken);
GoogleCredential
credential =
.
Builder
() .setTransport(
()) .setJsonFactory(
.getDefaultInstance()) .setClientSecrets(clientSecrets) .build(); credential.setAccessToken(accessToken); credential.setExpiresInSeconds(expiresInSeconds); credential.setRefreshToken(refreshToken);
Drive drive = new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential) .setApplicationName("Auth Code Exchange Demo") .build(); File file = drive.files().get("appfolder").execute();
Drive
drive =
.getDefaultInstance(), credential) .setApplicationName(
"Auth Code Exchange Demo"
) .build();
File
file = drive.files().
get
"appfolder"
).execute();