3

Question:

How does one define a Gmail API scope for a GoogleSignInOptions.Builder()?

Summary:

I have an Android app written in Kotlin, in which I am attempting to integrate a call to the Gmail API, though can not add the GMAIL_READONLY scope when requesting scopes.

Relevant Information & What I've tried:

From the documentation here:

...to configure Google Sign-In to request users' ID and basic profile information, create a GoogleSignInOptions object with the DEFAULT_SIGN_IN parameter. To request users' email addresses as well, create the GoogleSignInOptions object with the requestEmail option.

If you need to request additional scopes to access Google APIs, specify them with requestScopes.

I have in my application gradle file the Google API client and Gmail service dependencies taken from the Java Gmail Quickstart:

implementation 'com.google.api-client:google-api-client:1.23.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
implementation 'com.google.apis:google-api-services-gmail:v1-rev83-1.23.0'

And in the corresponding fragment I have a setOnClickListener{} for a button which is to make the API call. Following the steps on the previously linked "Integrating Google Sign-In into Your Android App" page, I have the following code snippet:

view.findViewById<Button>(R.id.google_sign_in_button).setOnClickListener {
            val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build()
}

I am however having build errors when I attempt to add the Gmail.READONLY scope.

Examples:

Using the following declaration

val gmailScope = Scope(GmailScopes.GMAIL_READONLY)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestEmail()
    .requestScopes(GmailScopes.GMAIL_READONLY)
    .build()

gives a Type mismatch error, for which the required type is Scope and GmailScopes.GMAIL_READONLY is of type String. The Gmail Java Quickstart uses a String List for defininf scopes with:

Collections.singletonList(GmailScopes.GMAIL_LABELS);

However integrating this with

.requestScopes(Collections.singletonList(GmailScopes.GMAIL_LABELS))

gives the same type error, naturally with a Mutable List<String> instead of the previous String.

The Scope class as documented on this Google APIs for Android page defines the constructor to be:

Scope(String scopeUri): Creates a new scope with the given URI.

So I attempted an implementation using this class as such:

val gmailScope = Scope(GmailScopes.GMAIL_READONLY)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestEmail()
    .requestScopes(gmailScope)
    .build()

and also directly with the URI:

val gmailScope = Scope("https://www.googleapis.com/auth/gmail.readonly")

However this gives an Unresolved refernce: Scope error. I followed the advice from this question but to no avail.

Community
  • 1
  • 1
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54

1 Answers1

1

Answer:

The .kt file is missing the Scope class import from android.gms.common.api.

Import line:

import com.google.android.gms.common.api.Scope
Community
  • 1
  • 1
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54