2016-01-11 70 views
1

我在SignActivity使用Google SignIn如下:爲什麼我無法創建日曆?

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestIdToken(getString(R.string.server_client_id)) 
      .requestEmail() 
      .requestProfile() 
      .build(); 
// [END configure_signin] 

// [START build_client] 
// Build a GoogleApiClient with access to the Google Sign-In API and the 
// options specified by gso. 
mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 
// [END build_client] 

SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); 
signInButton.setSize(SignInButton.SIZE_WIDE); 
signInButton.setScopes(gso.getScopeArray()); 

現在我能得到令牌,電子郵件名稱和其它信息。

而且我也跟着this example在下面我的其他活動,以創建指定的電子郵件日曆:

//for calendar start 
static final int REQUEST_ACCOUNT_PICKER = 1000; 
static final int REQUEST_AUTHORIZATION = 1001; 
static final int REQUEST_GOOGLE_PLAY_SERVICES = 1002; 
private static final String PREF_ACCOUNT_NAME = "accountName"; 
private static final String[] SCOPES = { CalendarScopes.CALENDAR }; 
GoogleAccountCredential mCredential; 
com.google.api.services.calendar.model.Calendar calendar; 
//for calendar end 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ... 
    createBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // I take the email from my sign activity. 
      String emailName = SignActivity.getPref(PREF_ACCOUNT_NAME, getApplicationContext()); 
      mCredential = GoogleAccountCredential.usingOAuth2(
        getApplicationContext(), Arrays.asList(SCOPES)) 
        .setBackOff(new ExponentialBackOff()) 
        .setSelectedAccountName(emailName); 
      if (isDeviceOnline()) { 
       new MakeRequestTask(mCredential).execute(); 
      } else { 
       descriptionEditText.setText("No network connection available."); 
      } 
     } 
    }); 
    ... 
} 

和我MakeRequestTask構造:

HttpTransport transport = AndroidHttp.newCompatibleTransport(); 
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 
mService = new Calendar.Builder(
     transport, jsonFactory, credential) 
     .setApplicationName("Test") 
     .build(); 

,並和我的問題是在doInBackground功能在在try塊中的第一行它不能執行和做其他代碼,但奇怪我沒有得到任何錯誤:

try{ 
    com.google.api.services.calendar.model.Calendar createdCalendar = mService.calendars().insert(calendar).execute(); 

    CalendarListEntry calendarListEntry = new CalendarListEntry(); 
    calendarListEntry.setId(createdCalendar.getId()); 
    calendarListEntry.setBackgroundColor(backGroundColor); 
    calendarListEntry.setForegroundColor(foreGroundColor); 
    // Insert the new calendar list entry 
    CalendarListEntry createdCalendarListEntry = mService.calendarList().insert(calendarListEntry).execute(); 
     Log.i("summary", createdCalendarListEntry.getSummary()); 
    }catch (IOException e){ 
      Log.i("test1", e.getMessage()); 
    } 
+1

你有沒有試過Google的這個快速入門? https://developers.google.com/google-apps/calendar/quickstart/android – Andres

+0

是的,它的工作,但這使我再次登錄,但我已經登錄。我創建Web應用程序OAuth 2.0客戶端ID爲GoogleSignInOptions獲得ID令牌。這是一個問題嗎? – melomg

+1

Google Play服務8.3上的登錄功能發生了一些變化 - http://android-developers.blogspot.com/2015/11/whats-new-in-google-play-services-83.html – Andres

回答

1

在我createBtn的onClick功能

我加了下面代碼的其他代碼頂部:

我不知道這是真正的解決方案。但在我的舊代碼中,沒有要求許可日曆,但現在它要求我給予許可。

if (mCredential.getSelectedAccountName() == null) { 
    chooseAccount(); 
} 

現在它正在發揮作用。

相關問題