如何使用Google+登录按钮访问Google Calendar API?

时间:2014-12-19 14:52:58

标签: android eclipse google-plus google-calendar-api google-oauth

这是我第一次在这里提问,所以如果我以错误的格式写问题,我很抱歉。所以我在我的Android应用中使用了Google+登录按钮(使用Eclipse),就像在这个链接中https://developers.google.com/+/mobile/android/sign-in一样,但我不明白如何使用它来访问Google Calendar API。以下是我正在尝试实施的内容的参考:https://developer.android.com/google/auth/http-auth.html。我理解这个概念,但是当我尝试使用它时,没有任何反应。这是代码:

public class GetTask extends AsyncTask<Void, String, Void> {
    Activity mActivity;
    String mScope;
    String mEmail;
    String mToken;

    GetTask(Activity activity, String name, String scope) {
        this.mActivity = activity;
        this.mScope = scope;
        this.mEmail = name;
    }

    /**
     * Executes the asynchronous job. This runs when you call execute()
     * on the AsyncTask instance.
     */
    @Override
    protected Void doInBackground(Void... params) {
        try {
            String mToken = fetchToken();
            if (mToken != null) {
                GoogleCredential credential = new GoogleCredential().setAccessToken(mToken);
                HttpTransport httpTransport = new NetHttpTransport();
                JsonFactory jsonFactory = new JacksonFactory();
                calGoogle = new Calendar.Builder(httpTransport, jsonFactory, credential).
                            setApplicationName("Calendar View").build();
                Log.d("Token", mToken);

                // Create and initialize a new event
                Event event = new Event();
                event.setSummary("Appointment");
                event.setLocation("Somewhere");

                ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
                attendees.add(new EventAttendee().setEmail("attendeeEmail"));
                event.setAttendees(attendees);

                Date startDate = new Date(System.currentTimeMillis());
                Date endDate = new Date(startDate.getTime() + 3600000);
                DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
                event.setStart(new EventDateTime().setDateTime(start));
                DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
                event.setEnd(new EventDateTime().setDateTime(end));

                // Insert the new event
                Event createdEvent = calGoogle.events().insert("primary", event).execute();
            }
        } catch (IOException e) {
            // The fetchToken() method handles Google-specific exceptions,
            // so this indicates something went wrong at a higher level.
            // TIP: Check for network connectivity before starting the AsyncTask.

        }
        return null;
    }

    /**
     * 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.
            //mActivity.handleException(userRecoverableException);
        } catch (GoogleAuthException fatalException) {
            // Some other type of unrecoverable exception has occurred.
            // Report and log the error as appropriate for your app.

        }
        return null;
    }
}    

我想将事件添加到Google日历,但它没有做任何事情。我已经检查了LogCat,但它看起来很正常。我的问题是,我们如何使用Google+登录按钮而不是oauth2.0来使用Google API?谷歌建议对Android应用程序,但我不明白这个解释。

0 个答案:

没有答案