Example usage for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential usingAudience

List of usage examples for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential usingAudience

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential usingAudience.

Prototype

public static GoogleAccountCredential usingAudience(Context context, String audience) 

Source Link

Document

Sets the audience scope to use with Google Cloud Endpoints.

Usage

From source file:com.google.cloud.backend.core.CloudBackendActivity.java

License:Apache License

/**
 * Subclasses may override this to execute initialization of the activity. If
 * it uses any CloudBackend features, it should be executed inside
 * {@link #onPostCreate()} that will be called after CloudBackend
 * initializations such as user authentication.
 *//*from w  ww .j a  v a2 s  .  c o m*/
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // init com.google.cloud.backend
    cloudBackend = new CloudBackendMessaging(this);

    // create credential
    credential = GoogleAccountCredential.usingAudience(this, Consts.AUTH_AUDIENCE);
    cloudBackend.setCredential(credential);

    // if auth enabled, get account name from the shared pref
    if (isAuthEnabled()) {
        String accountName = cloudBackend.getSharedPreferences().getString(PREF_KEY_ACCOUNT_NAME, null);
        if (accountName == null) {
            // let user pick an account
            super.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
            return; // continue init in onActivityResult
        } else {
            credential.setSelectedAccountName(accountName);
        }
    }

    // post create initialization
    _onPostCreate();
}

From source file:com.google.cloud.backend.neednow.core.CloudBackendActivity.java

License:Apache License

/**
 * Subclasses may override this to execute initialization of the activity. If
 * it uses any CloudBackend features, it should be executed inside
 * {@link #onPostCreate()} that will be called after CloudBackend
 * initializations such as user authentication.
 *//*from   w  ww .  j  a v  a 2  s . c o  m*/
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // init backend
    cloudBackend = new CloudBackendMessaging(this);

    // create credential
    credential = GoogleAccountCredential.usingAudience(this.getApplicationContext(), Consts.AUTH_AUDIENCE);
    cloudBackend.setCredential(credential);

    // if auth enabled, get account name from the shared pref
    if (isAuthEnabled()) {
        String accountName = cloudBackend.getSharedPreferences().getString(PREF_KEY_ACCOUNT_NAME, null);
        if (accountName == null) {
            // let user pick an account
            super.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
            return; // continue init in onActivityResult
        } else {
            credential.setSelectedAccountName(accountName);
        }
    }

    // post create initialization
    _onPostCreate();
}

From source file:com.google.cloud.backend.neednow.core.CloudBackendFragment.java

License:Apache License

/**
 * Subclasses may override this to execute initialization of the activity.
 * If it uses any CloudBackend features, it should be executed inside
 * {@link #onCreateFinished()} that will be called after CloudBackend
 * initializations such as user authentication.
 *//*from w w  w. jav a2  s.co m*/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mCloudBackend = new CloudBackendAsync(getActivity());

    // create credential
    mCredential = GoogleAccountCredential.usingAudience(getActivity(), Consts.AUTH_AUDIENCE);
    mCloudBackend.setCredential(mCredential);
    signInAndSubscribe(false);

}

From source file:com.google.cloud.solutions.griddler.android.GameApplication.java

License:Open Source License

private void init() {
    Log.d(TAG, "initializing application components");

    // to prevent EOFException after idle
    // http://code.google.com/p/google-http-java-client/issues/detail?id=116
    System.setProperty("http.keepAlive", "false");

    mCredential = GoogleAccountCredential.usingAudience(this, GameBackendSettings.AUDIENCE_ID);
    mSettings = new ApplicationSettings(this.getApplicationContext());

    initializeGoogleServices();/*  w  ww .ja v a  2 s  . c  om*/
}

From source file:com.google.cloud.solutions.griddler.android.providers.DataProvider.java

License:Open Source License

/**
 * Constructor//from   w  w w.ja  v  a  2  s  .  c om
 *
 * @param context The context
 */
public DataProvider(Context context) {
    settings = new ApplicationSettings(context);

    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context,
            GameBackendSettings.AUDIENCE_ID);
    credential.setSelectedAccountName(settings.getSelectedAccountName());

    Griddler.Builder builder = new Griddler.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),
            credential);
    builder.setApplicationName(context.getString(R.string.app_name));
    builder.setRootUrl(GameBackendSettings.DEFAULT_ROOT_URL);
    service = builder.build();
}

From source file:com.google.cloud.solutions.smashpix.MainActivity.java

License:Open Source License

  @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  createImageStorageDirectory();//w  w w . java2 s  .c  om
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  Logger.getLogger(LOGGER_NAME).setLevel(Constants.LOGGING_LEVEL);

  AccountManager accountManager = AccountManager.get(this);
  Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
  accountName = accounts[0].name;

  credential =
      GoogleAccountCredential.usingAudience(this, AUDIENCE_NAMESPACE + Constants.WEB_CLIENT_ID);
  credential.setSelectedAccountName(accountName);
  Image.Builder builder = new Image.Builder(
    AndroidHttp.newCompatibleTransport(),
    new GsonFactory(),
    credential);
  builder.setApplicationName(Constants.APP_NAME);
  service = builder.build();

  listImages();
}

From source file:com.google.devrel.samples.helloendpoints.MainActivity.java

License:Open Source License

/**
 * This method is invoked when the "Get Authenticated Greeting" button is clicked. See
 * activity_main.xml for the dynamic reference to this method.
 *//*  w w w .  j a  va2s .  c  om*/
public void onClickGetAuthenticatedGreeting(View unused) {
    if (!isSignedIn()) {
        Toast.makeText(this, "You must sign in for this action.", Toast.LENGTH_LONG).show();
        return;
    }

    // Use of an anonymous class is done for sample code simplicity. {@code AsyncTasks} should be
    // static-inner or top-level classes to prevent memory leak issues.
    // @see http://goo.gl/fN1fuE @26:00 for an great explanation.
    AsyncTask<Void, Void, HelloGreeting> getAuthedGreetingAndDisplay = new AsyncTask<Void, Void, HelloGreeting>() {
        @Override
        protected HelloGreeting doInBackground(Void... unused) {
            if (!isSignedIn()) {
                return null;
            }
            ;

            if (!AppConstants.checkGooglePlayServicesAvailable(MainActivity.this)) {
                return null;
            }

            // Create a Google credential since this is an authenticated request to the API.
            GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(MainActivity.this,
                    AppConstants.AUDIENCE);
            credential.setSelectedAccountName(mEmailAccount);

            // Retrieve service handle using credential since this is an authenticated call.
            Helloworld apiServiceHandle = AppConstants.getApiServiceHandle(credential);

            try {
                Authed getAuthedGreetingCommand = apiServiceHandle.greetings().authed();
                HelloGreeting greeting = getAuthedGreetingCommand.execute();
                return greeting;
            } catch (IOException e) {
                Log.e(LOG_TAG, "Exception during API call", e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(HelloGreeting greeting) {
            if (greeting != null) {
                displayGreetings(greeting);
            } else {
                Log.e(LOG_TAG, "No greetings were returned by the API.");
            }
        }
    };

    getAuthedGreetingAndDisplay.execute((Void) null);
}

From source file:com.google.devrel.samples.memedroid.app.Constants.java

License:Apache License

/**
 * Retrieve a GoogleAccountCredential used to authorise requests made to the cloud endpoints
 * backend./*w  w w . j  av  a  2  s.  c  o  m*/
 *
 * @param context application context
 * @param accountName the account selected
 * @return
 */
public static GoogleAccountCredential getCredential(Context context, String accountName) {
    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context,
            "server:client_id:" + Constants.SERVER_CLIENTID);
    // Small workaround to avoid setting an account that doesn't exist, so we can test.
    if (!TEST_EMAIL_ADDRESS.equals(accountName)) {
        credential.setSelectedAccountName(accountName);
    }
    return credential;
}

From source file:com.google.sample.junk.SignInActivity.java

License:Open Source License

/**
 * Retrieves the previously used account name from the application
 * preferences and checks if  the credential object can be set to this
 * account.//from  w  ww .ja  va 2 s. c o m
 * @return a boolean indicating if the user is signed in or not
 */
private boolean isSignedIn() {
    credential = GoogleAccountCredential.usingAudience(this, Constants.AUDIENCE_ANDROID_CLIENT_ID);
    SharedPreferences settings = getSharedPreferences("MobileAssistant", 0);
    String accountName = settings.getString(ACCOUNT_NAME_SETTING_NAME, null);
    credential.setSelectedAccountName(accountName);
    return credential.getSelectedAccount() != null;
}

From source file:com.google.samples.quickstart.signin.MainActivity.java

License:Open Source License

@Override
protected String doInBackground(Pair<Context, String>... params) {

    context = params[0].first;/*from   ww w .ja v  a 2  s.  c o m*/
    email = params[0].second;
    credential = GoogleAccountCredential.usingAudience(context,
            "server:client_id:**************************.apps.googleusercontent.com");
    credential.setSelectedAccountName(email);

    Log.d("credential", credential.getSelectedAccountName());

    if (myApiService == null) { // Only do this once
        MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), credential)
                        // options for running against local devappserver
                        // - 10.0.2.2 is localhost's IP address in Android emulator
                        // - turn off compression when running against local devappserver
                        .setRootUrl("http://10.0.2.2:8080/_ah/api/")
                        .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                            @Override
                            public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest)
                                    throws IOException {
                                abstractGoogleClientRequest.setDisableGZipContent(true);
                            }
                        });
        // end options for devappserver

        myApiService = builder.build();
    }

    try {
        return myApiService.sayHi().execute().getData();
    } catch (IOException e) {
        return e.getMessage();
    }
}