Android Open Source - Google-Plus-Android-Sample O Auth Activity






From Project

Back to project page Google-Plus-Android-Sample.

License

The source code is released under:

Apache License

If you think the Android project Google-Plus-Android-Sample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* 
 * Copyright (C) 2011 iMellon/*  ww  w  .j  a v a 2 s  .c  om*/
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.imellon.android.googleplus;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.imellon.android.googleplus.store.SharedPreferencesCredentialStore;

/**
 * @author Christos Papazafeiropoulos, Dimitris Makris
 * 
 * This class starts a webview to let the user either insert credentials 
 * and accept/deny the application or accept/deny directly the application.
 * The procedure is based on the OAuth 2.0 dance which is handled by the 
 * Google libraries.
 * 
 */

public class OAuthActivity extends Activity {

  private SharedPreferences prefs;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // new OAuthRequestTokenTask(this).execute();
  }

  @Override
  protected void onResume() {
    super.onResume();
    WebView webview = new WebView(this);
    webview.setVisibility(View.VISIBLE);
    webview.getSettings().setJavaScriptEnabled(true);
    setContentView(webview);
    
    String googleAuthorizationRequestUrl = new GoogleAuthorizationRequestUrl(
        SharedPreferencesCredentialStore.CLIENT_ID,
        SharedPreferencesCredentialStore.REDIRECT_URI,
        SharedPreferencesCredentialStore.SCOPE).build();

    webview.setWebViewClient(new WebViewClient() {
 
      @Override
      public void onPageFinished(WebView view, String url) {

        if (url.startsWith(SharedPreferencesCredentialStore.REDIRECT_URI)) {
          try {
            if (url.indexOf("code=") != -1) {
              // Url is like http://localhost/?code=4/Z5DgC1IxNL-muPsrE2Sjy9zQn2pF
              String code = url.substring(
                  SharedPreferencesCredentialStore.REDIRECT_URI.length() + 7,
                  url.length());

              AccessTokenResponse accessTokenResponse = new GoogleAuthorizationCodeGrant(
                  new NetHttpTransport(),
                  new JacksonFactory(),
                  SharedPreferencesCredentialStore.CLIENT_ID,
                  SharedPreferencesCredentialStore.CLIENT_SECRET,
                  code, SharedPreferencesCredentialStore.REDIRECT_URI)
                  .execute();

              SharedPreferencesCredentialStore credentialStore = SharedPreferencesCredentialStore
                  .getInstance(prefs);
              credentialStore.write(accessTokenResponse);
              view.setVisibility(View.INVISIBLE);
              startActivity(new Intent(
                  OAuthActivity.this,
                  GooglePlusActivity.class));
              finish();
            } else if (url.indexOf("error=") != -1) {
              view.setVisibility(View.INVISIBLE);
              SharedPreferencesCredentialStore.getInstance(prefs)
                  .clearCredentials();
              startActivity(new Intent(
                  OAuthActivity.this,
                  GooglePlusActivity.class));
              finish();
            }

          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    });

    webview.loadUrl(googleAuthorizationRequestUrl);
  }
}




Java Source Code List

com.imellon.android.googleplus.GooglePlusActivity.java
com.imellon.android.googleplus.MyProfileActivity.java
com.imellon.android.googleplus.OAuthActivity.java
com.imellon.android.googleplus.ProfileActivitiesActivity.java
com.imellon.android.googleplus.ProfilesActivity.java
com.imellon.android.googleplus.store.SharedPreferencesCredentialStore.java
com.imellon.android.googleplus.utils.ProfileActivitiesAdapter.java
com.imellon.android.googleplus.utils.ProfilesAdapter.java