Google Calender API OAuth2.0

时间:2015-11-05 20:56:33

标签: java android oauth-2.0 google-calendar-api google-oauth

在使用Google Calendar API尝试在Google日历中插入和发送事件时,我似乎未能授权使用OAuth2.0。

由于某些原因,问题似乎发生在这段代码中的某个地方,并最终抛出了一个异常,该异常在 GoogleAuthException 中被捕获。值得注意的是,如果我删除了try / catch块,它将成功到达CreateCalenderEventTask中的 event.insert (稍后被调用)但会被 UserRecoverableAuthIOException <捕获/ strong>而不是。

    try {
        // If the application has the appropriate access then a token will be retrieved, otherwise
        // an error will be thrown.
        mCredential = GoogleAccountCredential.usingOAuth2(mActivity,
                                                                                                            Arrays.asList(GoogleCalendarUtils.SCOPES));
        mCredential.setSelectedAccountName(emailAccount);
        String accessToken = mCredential.getToken();
        Log.e(TAG, "ACCESS TOKEN" + accessToken);
        // Success.
        return true;
    } catch (GoogleAuthException unrecoverableException) {
        // Failure.
        Log.e(TAG, "UNRECOVERABLE: " + Log.getStackTraceString(unrecoverableException));
        return false;
    } catch (IOException ioException) {
        // Failure or cancel request.
        Log.e(TAG, "IO EXCEPTION: " + Log.getStackTraceString(ioException));
        return false;
    }

GoogleCalendarUtils - 使用此类处理OAUTH和事件插入的异步请求

public class GoogleCalendarUtils {

    private static String TAG = GoogleCalendarUtils.class.getCanonicalName();

    //CALENDAR REQUEST
    public static final int REQUEST_ACCOUNT_PICKER = 1000;
    public static final int REQUEST_AUTHORIZATION = 1001;
    public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1002;
    public static final String PREF_ACCOUNT_NAME = ""; //possibly retrieve this data from the server, or store your preferred account in shared prefs
    public static final String[] SCOPES = { CalendarScopes.CALENDAR };

    public static final JsonFactory JSON_FACTORY = new AndroidJsonFactory();
    public static final HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
    private static AuthorizationCheckTask mAuthTask;
    public static GoogleAccountCredential mCredential = null;

    public static Calendar getCalendarServiceHandle(@Nullable GoogleAccountCredential credential) {
        return new Calendar.Builder(
                                                                HTTP_TRANSPORT, JSON_FACTORY, credential).build();
    }

    public static int countGoogleAccounts(Context context) {
        AccountManager am = AccountManager.get(context);
        Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
        if (accounts == null || accounts.length < 1) {
            return 0;
        } else {
            return accounts.length;
        }
    }

    public static ArrayList<Account> getGoogleAccounts(Context context) {
        AccountManager am = AccountManager.get(context);
        Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
        ArrayList<Account> accountArray = new ArrayList<Account>();
        for (Account acct : accounts) {
            accountArray.add(acct);
        }

        return accountArray;
    }

    public static boolean checkGooglePlayServicesAvailable(Activity activity) {
        final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
        if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
            showGooglePlayServicesAvailabilityErrorDialog(activity, connectionStatusCode);
            return false;
        }
        return true;
    }

    public static void showGooglePlayServicesAvailabilityErrorDialog(final Activity activity,
                                                                                                                                        final int connectionStatusCode) {
        final int REQUEST_GOOGLE_PLAY_SERVICES = 0;
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
                                                                                                                            connectionStatusCode, activity,
                                                                                                                            REQUEST_GOOGLE_PLAY_SERVICES);
                dialog.show();
            }
        });
    }

    public static class CreateCalendarEventTask extends AsyncTask<Void, Void, Void> {
        private Calendar mService = null;
        Reservation reservation = null;

        public CreateCalendarEventTask(GoogleAccountCredential credential, Reservation res) {
            if (res != null) {
                this.reservation = res;
            }
            mService = getCalendarServiceHandle(credential);
        }

        @Override
        protected Void doInBackground(Void... params) {
            createCalendarEvent();
            return null;
        }

        @Override
        protected void onCancelled() {}

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        }

        private void createCalendarEvent() {
            Event event = new Event()
                                                                .setSummary("TEST EVENT 11/7/2015")
                                                                .setLocation("800 Howard St., San Francisco, CA 94103")
                                                                .setDescription("A chance to hear more about Google's developer products.");

            Log.e(TAG, reservation.getReqDatetime().toString());

            DateTime startDateTime = new DateTime("2015-11-07T09:00:00-07:00");
            EventDateTime start = new EventDateTime()
                .setDateTime(startDateTime)
                .setTimeZone("America/Los_Angeles");
            event.setStart(start);

            DateTime endDateTime = new DateTime("2015-11-07T17:00:00-07:00");
            EventDateTime end = new EventDateTime()
                .setDateTime(endDateTime)
                .setTimeZone("America/Los_Angeles");
            event.setEnd(end);

            String calendarId = "primary";
            try {
                Log.e(TAG, "ABOUT TO INSERT THE EVENT");
                event = mService.events().insert(calendarId, event).execute();
            } catch (UserRecoverableAuthIOException e1) {
                Log.e(TAG, Log.getStackTraceString(e1));
            } catch (IOException e) {}
        }
    }

    public static void performAuthCheck(Activity activity, String emailAccount, AuthorizationCheckCallback callback) {
        // Cancel previously running tasks.
        if (mAuthTask != null) {
            try {
                mAuthTask.cancel(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        new AuthorizationCheckTask(activity, callback).execute(emailAccount);
    }

    public static class AuthorizationCheckTask extends AsyncTask<String, Integer, Boolean> {

        Activity mActivity;
        AuthorizationCheckCallback mCallback;

        public AuthorizationCheckTask(Activity activity, AuthorizationCheckCallback callback) {
            this.mActivity = activity;
            this.mCallback = callback;
        }

        @Override
        protected Boolean doInBackground(String... emailAccounts) {

            if (!checkGooglePlayServicesAvailable(mActivity)) { return false; }

            String emailAccount = emailAccounts[0];
            // Ensure only one task is running at a time.
            mAuthTask = this;

            // Ensure an email was selected.
            if (Strings.isNullOrEmpty(emailAccount)) {
                // Failure.
                return false;
            }

            try {
                // If the application has the appropriate access then a token will be retrieved, otherwise
                // an error will be thrown.
                mCredential = GoogleAccountCredential.usingOAuth2(mActivity,
                                                                                                                    Arrays.asList(GoogleCalendarUtils.SCOPES));
                mCredential.setSelectedAccountName(emailAccount);
                String accessToken = mCredential.getToken();
                Log.e(TAG, "ACCESS TOKEN" + accessToken);
                // Success.
                return true;
            } catch (GoogleAuthException unrecoverableException) {
                // Failure.
                Log.e(TAG, "UNRECOVERABLE: " + Log.getStackTraceString(unrecoverableException));
                return false;
            } catch (IOException ioException) {
                // Failure or cancel request.
                Log.e(TAG, "IO EXCEPTION: " + Log.getStackTraceString(ioException));
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean success) {
            if (success) {
                // Authorization check successful, set internal variable.
                mCallback.onSuccess(mCredential);
            } else {
                // Authorization check unsuccessful.
                mCallback.onFailure();
            }
            mAuthTask = null;
        }

        @Override
        protected void onCancelled() {
            mAuthTask = null;
        }

        public interface AuthorizationCheckCallback {
            void onSuccess(GoogleAccountCredential credential);

            void onFailure();
        }
    }
}

STACK TRACE:

    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils$AuthorizationCheckTask.doInBackground(GoogleCalendarUtils.java:198)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils$AuthorizationCheckTask.doInBackground(GoogleCalendarUtils.java:1)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    11-06 15:11:47.502: E/com.limosys.jlimomapprototype.utils.google.GoogleCalendarUtils(13925):    at java.lang.Thread.run(Thread.java:818)

0 个答案:

没有答案