本文整理匯總了Java中com.google.android.gms.auth.GoogleAuthUtil.getToken方法的典型用法代碼示例。如果您正苦於以下問題:Java GoogleAuthUtil.getToken方法的具體用法?Java GoogleAuthUtil.getToken怎麽用?Java GoogleAuthUtil.getToken使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.android.gms.auth.GoogleAuthUtil
的用法示例。
在下文中一共展示了GoogleAuthUtil.getToken方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
private void getToken(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String email = sharedPreferences.getString(Constants.KEY_EMAIL, null);
mUserGoogleId = sharedPreferences.getString(Constants.KEY_USER_GOOGLE_ID, null);
mUserUID = sharedPreferences.getString(Constants.KEY_USER_UID, null); //firebase userId
try {
String scope = String.format("oauth2:%s", "https://picasaweb.google.com/data/");
String token = null;
if (email != null) {
token = GoogleAuthUtil.getToken(context, email, scope);
}
Log.i(TAG, "Get Token\n" +
"2 Picasa client " + mPicasaClient + "\n" +
"2 Picasa service " + mPicasaService + "\n" +
"--- Google token " + token);
mPicasaClient.createService(token);
mPicasaService = mPicasaClient.getPicasaService();
} catch (IOException | GoogleAuthException e) {
e.printStackTrace();
}
}
示例2: authenticate
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
@Override
public Request authenticate(Route route, Response response) throws IOException {
Log.d(TAG, "Re-authenticating requests");
if (responseCount(response) >= 3) {
return null; // If we've failed 3 times, give up. - in real life, never give up!!
} else {
try {
if (mContext == null)
Log.d(TAG, "Context is null");
String token = GoogleAuthUtil.getToken(mContext, Utils.getLoginEmail(mContext), Constants.URL_SHORTNER_SCOPE);
Log.d(TAG, "New token is " + token);
Utils.setAuthToken(mContext, token);
return response.request().newBuilder()
.header("Authorization", "Bearer " + Utils.getAuthToken(mContext))
.build();
} catch (GoogleAuthException e) {
e.printStackTrace();
return null;
}
}
}
示例3: authenticate
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
protected String authenticate(Context context, String mGoogleUserName)
throws IOException, GoogleAuthException,
GooglePlayServicesAvailabilityException,
UserRecoverableAuthException {
// use google auth utils to get oauth2 token
String scope = "oauth2:https://www.googleapis.com/auth/mapsengine https://picasaweb.google.com/data/";
String token = null;
if (mGoogleUserName == null) {
Log.e(tag, "Google user not set");
return null;
}
token = GoogleAuthUtil.getToken(context, mGoogleUserName, scope);
return token;
}
示例4: fetchToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
/**
* Gets an authentication token from Google and handles any
* GoogleAuthException that may occur.
*/
protected String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present
// so we need to show the user some UI in the activity to recover.
handleException(userRecoverableException);
} catch (GoogleAuthException fatalException) {
// Some other type of unrecoverable exception has occurred.
// Report and log the error as appropriate for your app.
// LogHelper.LOGD("GoogleAuthProvider","GetUserNameTask: " + fatalException.getMessage());
// Toast.makeText(mActivity, fatalException.getMessage(), Toast.LENGTH_SHORT).show();
if (getAuthCallback() != null) {
getAuthCallback().onError(fatalException);
}
}
return null;
}
示例5: fetchToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
/**
* Gets an authentication token from Google and handles any
* GoogleAuthException that may occur.
*/
protected String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(MainActivity.this, mEmailText, mScope);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present
// so we need to show the user some UI in the activity to recover.
//mActivity.handleException(userRecoverableException);
//Log.e(TAG, userRecoverableException.getMessage(), userRecoverableException);
mException = userRecoverableException;
} catch (GoogleAuthException fatalException) {
// Some other type of unrecoverable exception has occurred.
// Report and log the error as appropriate for your app.
Log.e(TAG, fatalException.getMessage(), fatalException);
}
return null;
}
示例6: fetchToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
/**
* Fetches an access token from the G+ API
* @return the access token
* @throws IOException
*/
protected String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(
mContext,
Plus.AccountApi.getAccountName(mGoogleApiClient),
"oauth2:" + SCOPES);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present
// so we need to show the user some UI in the activity to recover.
Log.e(TAG, userRecoverableException.toString());
} catch (GoogleAuthException fatalException) {
// Some other type of unrecoverable exception has occurred.
// Report and log the error as appropriate for your app.
Log.e(TAG, fatalException.toString());
}
return null;
}
示例7: fetchToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
/**
* Get a authentication token if one is not available. If the error is not
* recoverable then it displays the error message on parent activity.
*/
private String fetchToken() {
try {
String token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
return token;
} catch (GooglePlayServicesAvailabilityException playEx) {
// GooglePlayServices.apk is either old, disabled, or not present.
mActivity.showErrorDialog(playEx.getConnectionStatusCode());
} catch (UserRecoverableAuthException userRecoverableException) {
// Unable to authenticate, but the user can fix this.
// Forward the user to the appropriate activity.
mActivity.startActivityForResult(userRecoverableException.getIntent(), mRequestCode);
} catch (GoogleAuthException fatalException) {
mActivity.showErrorDialog(ConnectionResult.INTERNAL_ERROR);
} catch (IOException e) {
mActivity.showErrorDialog(ConnectionResult.NETWORK_ERROR);
}
return null;
}
示例8: DriveSyncService
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
public DriveSyncService() throws UserRecoverableAuthException {
super(SyncService.GoogleDrive);
try {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
String token = GoogleAuthUtil.getToken(GlobalApplication.getAppContext(), Settings.getEmail(), "oauth2:" + DriveScopes.DRIVE_APPDATA);
if (SettingsUtil.shouldRefreshGDriveToken()) {
GoogleAuthUtil.clearToken(GlobalApplication.getAppContext(), token);
token = GoogleAuthUtil.getToken(GlobalApplication.getAppContext(), Settings.getEmail(), "oauth2:" + DriveScopes.DRIVE_APPDATA);
SettingsUtil.refreshGDriveToken();
}
if (BuildConfig.DEBUG)
LogUtil.log(getClass().getSimpleName(), "Access Token: " + token);
GoogleCredential credential = new GoogleCredential().setAccessToken(token);
service = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName("Narrate").build();
} catch (UserRecoverableAuthException ue) {
throw ue;
} catch (Exception e) {
LogUtil.log(getClass().getSimpleName(), "Exception in creation: " + e);
if (!BuildConfig.DEBUG) Crashlytics.logException(e);
}
if (CLEAR_FOLDER_CONTENTS) {
deleteEverything();
}
}
示例9: fetchToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
/**
* Gets an authentication token from Google and handles any
* GoogleAuthException that may occur.
*/
protected String fetchToken() throws IOException {
String accessToken;
try {
accessToken = GoogleAuthUtil.getToken(mActivity, mAccount, mScope);
GoogleAuthUtil.clearToken (mActivity, accessToken); // used to remove stale tokens.
accessToken = GoogleAuthUtil.getToken(mActivity, mAccount, mScope);
return accessToken;
} catch (UserRecoverableAuthException userRecoverableException) {
mActivity.startActivityForResult(userRecoverableException.getIntent(), mRequestCode);
} catch (GoogleAuthException fatalException) {
fatalException.printStackTrace();
}
return null;
}
示例10: onConnected
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
@Override
public void onConnected(Bundle connectionHint) {
mSignInClicked = false;
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String accessToken = null;
try {
accessToken = GoogleAuthUtil.getToken(AuthActivity.this, Plus.AccountApi.getAccountName(mGoogleApiClient),
"oauth2:" + Scopes.PROFILE + " " + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_MOMENTS + " " + Scopes.PLUS_ME);
} catch (IOException transientEx) {
transientEx.printStackTrace();
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), RC_RECOVER_AUTH);
} catch (GoogleAuthException authEx) {
authEx.printStackTrace();
}
return accessToken;
}
@Override
protected void onPostExecute(String token) {
authComplete(token);
}
};
task.execute();
}
示例11: fetchToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
protected String fetchToken() throws IOException {
// Log.i(TAG, "fetchToken");
try {
String tokenn=GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
// Log.i(TAG, tokenn);
return tokenn;
} catch (UserRecoverableAuthException userRecoverableException) {
// Log.i(TAG, "userRecoverableException");
mActivity.handleException(userRecoverableException);
} catch (GoogleAuthException fatalException) {
// Log.i(TAG, "fatalException");
fatalException.printStackTrace();
}
return null;
}
示例12: doInBackground
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
@Override
protected String doInBackground(GoogleApiClient... clients) {
if(clients.length == 0) return null;
GoogleApiClient client = clients[0];
String scopes = String.format("oauth2:%s", new Scope(Scopes.PROFILE));
try {
return GoogleAuthUtil.getToken(client.getContext(), Plus.AccountApi.getAccountName(client), scopes);
} catch(Exception exception) {
return null;
}
}
示例13: retrieveAuthToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
public String retrieveAuthToken(Account account)
throws GoogleAuthException, IOException {
String token = GoogleAuthUtil.getToken(mContext, account, SCOPE);
// Token needs to be clear so we make sure next time we get a brand new one. Otherwise this
// may return a token that has already been used by the API and because it's a one time
// token it won't work.
GoogleAuthUtil.clearToken(mContext, token);
return token;
}
示例14: fetchToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
/**
* Gets an authentication token from Google and handles any
* GoogleAuthException that may occur.
*/
protected String fetchToken() throws IOException {
if ( activity == null )
return null;
try {
logger.debug("Fetching google oauth2 token ...");
return GoogleAuthUtil.getToken(activity, mEmail, mScope);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present
// so we need to show the user some UI in the activity to recover.
logger.debug("User recoverable error occurred");
logger.error(userRecoverableException);
// Requesting an authorization code will always throw
// UserRecoverableAuthException on the first call to GoogleAuthUtil.getToken
// because the user must consent to offline access to their data. After
// consent is granted control is returned to your activity in onActivityResult
// and the second call to GoogleAuthUtil.getToken will succeed.
activity.startActivityForResult(userRecoverableException.getIntent(), REQUEST_AUTHORIZATION);
} catch (GoogleAuthException fatalException) {
logger.warn("google auth error occurred");
// Some other type of unrecoverable exception has occurred.
// Report and log the error as appropriate for your app.
logger.error(fatalException);
}
return null;
}
示例15: getToken
import com.google.android.gms.auth.GoogleAuthUtil; //導入方法依賴的package包/類
private boolean getToken() {
try{
token = GoogleAuthUtil.getToken(getActivity().getApplicationContext(), account, AuthActivity.SCOPE);
}catch (Exception e){
handleException(e);
return true;
}
return false;
}