MomenSherif / react-oauth

Google OAuth2 using the new Google Identity Services SDK for React 🚀
https://www.npmjs.com/package/@react-oauth/google
MIT License
1.11k stars 139 forks source link

How to use API client to make authorized requests using the access token? #329

Open AndiDog opened 9 months ago

AndiDog commented 9 months ago

Let's say the OAuth login using implicit flow works just fine, and an additional scope was requested successfully as well (as seen in the popup which asks for Drive permissions in this example):

const login = useGoogleLogin({
  onSuccess: (tokenResponse) => auth.setGoogleTokenResponse(tokenResponse),
  onError: (error) => console.error('Login Failed:', error),
  scope: 'https://www.googleapis.com/auth/drive.readonly',
});

Now after lots of digging and running into errors, I'm so incredibly confused with Google's deprecation of (parts of?) their JavaScript client libraries for browsers. I understood that the Google API Client Library for JavaScript is not deprecated, but only its authentication part. And since the latest react-oauth library loads the newer alternative, things should be fine. Nevertheless, when trying the client library as follows

gapi.load('client', () => {
        gapi.client
          .init({
            clientId: googleOauthClientId,
            scope: 'https://www.googleapis.com/auth/drive.readonly',
          })
          .then(() => {
            // ...Make request using the retrieved `tokenResponse.access_token` from above - but we don't even get here...
          })
          .catch((e) => console.error(e));
      });

I get this hard deprecation error in the catch clause:

You have created a new client application that uses libraries for user authentication or authorization that are deprecated. New clients must use the new libraries instead. See the Migration Guide for more information.

Any idea how to integrate actual API requests into a web app without using server-side requests? Adding an example would be very helpful for developers since Google really doesn't keep their documentation for JavaScript stuff clear and up to date.

alandsidel commented 3 months ago

Running into similar issues, did you manage to figure this out?