Reputation: 748
I used Google SignIn in my SignActivity as below:
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());
Now I am able to get token, email name and other informations.
And I followed this example to create a calendar for given email in my other activity as below :
//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.");
}
}
});
...
}
and my MakeRequestTask constructor :
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName("Test")
.build();
and and my problem is in doInBackground function at first line in the try block it can not execute and do other codes but oddly I don't get any error :
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());
}
Upvotes: 1
Views: 202
Reputation: 748
In my createBtn onClick function
I added below codes to the top of other codes:
I'm not sure this is the real solution. But in my old codes there were no ask for permission for calendar but now it is asking me to give a permission.
if (mCredential.getSelectedAccountName() == null) {
chooseAccount();
}
now it is working.
Upvotes: 1