title: android app beginner for writing code for google Calendar API
body: <p>I am a beginner at Android app design. I want to write a code to access the google calendar events and I have created authentication &amp; authorization in <a href="https://console.cloud.google" rel="nofollow noreferrer">https://console.cloud.google</a> and downloaded the client_secret_...json then put it in the res and src folder in android. Because Google Documents hasn't step by step to build an android app then I follow the java sample of calendarQuickstart to create my Gradle and Mainactivity.java as below.</p>
  <p>Gradle</p>
  <pre><code>plugins {
  
      id 'com.android.application'
  
  }
  
  android {
  
      compileSdk 31
      defaultConfig {
          applicationId &quot;com.example.mycalendar1&quot;
          minSdk 21
          targetSdk 31
          versionCode 1
          versionName &quot;1.0&quot;
          testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
      compileOptions {
          sourceCompatibility JavaVersion.VERSION_1_8
          targetCompatibility JavaVersion.VERSION_1_8
      }
  }
  // I use zulu8 
  
  
  dependencies {
  
      implementation 'androidx.appcompat:appcompat:1.4.0'
      implementation 'com.google.android.material:material:1.4.0'
      implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
      implementation files('libs/google-oauth-client-java6-1.32.1.jar')
      implementation files('libs/google-oauth-client-jetty-1.32.1.jar')
      implementation files('libs/google_http_client_gson-1.0.2.jar')
      testImplementation 'junit:junit:4.+'
      androidTestImplementation 'androidx.test.ext:junit:1.1.3'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation 'com.google.android.gms:play-services-auth:20.0.1'
      implementation 'pub.devrel:easypermissions:0.3.0'
      implementation('com.google.api-client:google-api-client-android:1.23.0') {
          exclude group: 'org.apache.httpcomponents'
      }
      implementation('com.google.apis:google-api-services-calendar:v3-rev125-1.20.0') {
          exclude group: 'org.apache.httpcomponents'
      }
  }
      
  </code></pre>
  <p>Mainactivity.java</p>
  <pre><code>package com.example.mycalendar1;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import com.google.api.client.auth.oauth2.Credential;
  
  import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
  
  import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
  
  import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
  
  import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
  
  import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
  
  import com.google.api.client.http.javanet.NetHttpTransport;
  
  import com.google.api.client.json.JsonFactory;
  
  import com.google.api.client.json.gson.GsonFactory;
  
  import com.google.api.client.util.DateTime;
  
  import com.google.api.client.util.store.FileDataStoreFactory;
  
  import com.google.api.services.calendar.Calendar;
  
  import com.google.api.services.calendar.CalendarScopes;
  
  import com.google.api.services.calendar.model.Event;
  
  import com.google.api.services.calendar.model.Events;
  
  import android.os.Bundle;
  
  import android.widget.TextView;
  
  import java.io.FileNotFoundException;
  
  import java.io.IOException;
  
  import java.io.InputStream;
  
  import java.io.InputStreamReader;
  
  import java.security.GeneralSecurityException;
  
  import java.util.Collections;
  
  import java.util.List;
  
  
  public class MainActivity extends AppCompatActivity {
  
      /** Application name. */
      private static final String APPLICATION_NAME = &quot;Google Calendar API Java Quickstart&quot;;
      /** Global instance of the JSON factory. */
      private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
      private static final String TOKENS_DIRECTORY_PATH = &quot;tokens&quot;;
  
      /**
       * Global instance of the scopes required by this quickstart.
       * If modifying these scopes, delete your previously saved tokens/ folder.
       */
      private static final List&lt;String&gt; SCOPES = Collections.singletonList(CalendarScopes.CALENDAR_READONLY);
      private static final String CREDENTIALS_FILE_PATH = &quot;/credentials.json&quot;;
  
      /**
       * Creates an authorized Credential object.
       * @param HTTP_TRANSPORT The network HTTP Transport.
       * @return An authorized Credential object.
       * @throws IOException If the credentials.json file cannot be found.
       */
      private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws
              IOException {
          // Load client secrets.
          InputStream in = MainActivity.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
          if (in == null) {
              throw new FileNotFoundException(&quot;Resource not found: &quot; + CREDENTIALS_FILE_PATH);
          }
          GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
  
          // Build flow and trigger user authorization request.
          GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                  HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                  .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                  .setAccessType(&quot;offline&quot;)
                  .build();
          LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
          Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize(&quot;user&quot;);
          //returns an authorized Credential object.
          return credential;
      }
  
  
  
      private static TextView tv1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          tv1 = findViewById(R.id.txt1);
  
          final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
  
          Calendar service = null;
          try {
              service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                      .setApplicationName(APPLICATION_NAME)
                      .build();
          } catch (IOException e) {
              e.printStackTrace();
          }
          
  
          Event event = null;
          try {
              event = service.events().get(&quot;***CalendarID&quot;, &quot;***EventID&quot;).execute();
          } catch (IOException e) {
              e.printStackTrace();
          }
          tv1.setText(event.getSummary().toString());
      }
  }
  </code></pre>
  <p>only a text view tv1 in my activity_main.xml just shows the event summary.</p>
  <p>when I run it, then I got a lot of errors.</p>
  <p>Can anybody tell me how to use google calendar API in android? it bothers me so many days.</p>
  <p>Thanks a lot.</p>