Estou tentando obter uma lista de calendários utilizando o Calendar API da google. A autenticação com o google funciona perfeitamente, porém, ao solicitar a lista de calendários eu recebo um erro de código 401. O que pode estar fazendo a minha chamada estar retornando esse erro?
Call<JsonObject> call = RetrofitClient.googleService().listCalendars(
token, getResources().getString(R.string.google_maps_key));
@GET("calendar/v3/users/me/calendarList")
Call<JsonObject> listCalendars(
@Header("Authorization") String token,
@Query("key") String key);
Minha instancia do Retrofit:
public static Retrofit getInstanceGoogle(){
if(retrofitGoogle == null) {
return retrofitGoogle = new Retrofit.Builder()
.baseUrl("https://www.googleapis.com/")
.addConverterFactory(new NullOnEmptyConverterFactory())
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
.addConverterFactory(ScalarsConverterFactory.create())
.client(new OkHttpClient.Builder()
.readTimeout(180, TimeUnit.SECONDS)
.writeTimeout(180, TimeUnit.SECONDS)
.connectTimeout(180, TimeUnit.SECONDS)
.build())
.build();
}
return retrofitGoogle;
}
O erro que é retornado:
{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Login Required.", "domain": "global", "reason": "required", "location": "Authorization", "locationType": "header" } ], "status": "UNAUTHENTICATED" } }
Tela de autenticação:
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
SignInButton signInButton;
GoogleSignInClient mGoogleSignInClient;
public static final int RC_SIGN_IN = 930;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);
signInButton = findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(this);
Scope scope1 = new Scope(CalendarScopes.CALENDAR);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Fitness.SCOPE_ACTIVITY_READ, scope1)
.requestIdToken(CLIENT_ID)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
User user = new User();
user.setUserEmail(account.getEmail());
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
finish();
} catch (ApiException e) {
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
}
}