Back

Getting the underlying OAuth Http Client for usage with e.g. Google Calendar Api

  • 0
  • Flutter
  • Auth
CalSap
13 Aug, 2024, 18:04

I am currently having difficulties using the already authenticated OAuth user and making calls to other APIs like the Google Calendar API to e.g. get the calendar entries. I have successfully authenticated the user using OAuth2 doing the following:

TypeScript
Client client = Client()
      .setEndpoint('url')
      .setProject('id');
Account account = Account(client);
await account.createOAuth2Session(
      provider: OAuthProvider.google,
      success: "http://localhost:8080/auth/oauth2/success",
      failure: "http://localhost:8080/auth/oauth2/failure",
      scopes: ['openid', 'https://www.googleapis.com/auth/calendar'],
    );

I now have access to the current user and the session:

TypeScript
loggedInUser = await account.get();
currentSession = await account.getSession(sessionId: 'current');

I now want to use this authenticated OAuth client to make requests (and also handle refresh tokens) to other APIs. The Google Calendar API for example takes a modified http client that sends the Authorization header with every request. For example:

TypeScript
var googleCalendar = CalendarApi(<http.Client>);
calendarList = await googleCalendar.calendarList.list();

My question is: How can I get the already authorized httpClient from the Appwrite SDK so that i can use it to call other SDKs and also make common REST API calls using the http client itself? I see that the Client object has a method call() which uses the internal httpClient to do exactly what I describe (Using the underlying httpClient to make authorized requests). I am currently doing the following as a workaround and it gets pretty messy handling the refresh and scopes for both flutter libraries (Using the OAuth Flutter Package: https://pub.dev/packages/oauth2):

TypeScript
currentOAuth2Client = oauth2.Client(
  oauth2.Credentials(
    currentSession.providerAccessToken,
    ...
  ),
);
TL;DR
In order to use an already authorized OAuth client for Google Calendar API, developers can use the underlying HTTP client from the Appwrite SDK. This can be achieved by calling `client.call()` to make authorized requests. Avoid using workarounds like creating a new OAuth2 client manually, as it can get messy.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more