firebasefirebase-authenticationgoogle-drive-apigoogle-oauthnativescript-firebase

How to use Firebase IdToken to generate OAuth Access for Google drive?


Update

In my Nativescript-Angular application I'm using @nativescript/firebase plugin which uses Firebase authentication and in the scope I have provided https://www.googleapis.com/auth/drive.

firebase
        .login({
          type: firebase.LoginType.GOOGLE,
          googleOptions: {
            scopes: ["email", "https://www.googleapis.com/auth/drive"],
          },
        })
        .then((user: firebase.User) => {
          //Firebase ID Token
          const idToken = value.providers[0].token;
        })
        .catch((err) => {
          console.log(err);
        });

While running application, it asked for user login along with user's consent for Google drive access. After successful authentication it returns a Firebase Id Token.

In the next step application requires to access user's Google Drive. As I know Google Drive API requires OAuth Access token while application has Firebase Token.

https://www.googleapis.com/drive/v3/files

My questions is: How to generate OAuth Access token which is capable to access user's Google drive? OR Is there any alternate solution for this?


Solution

  • From what I can determine, the FirebaseNativeScript library's getAuthToken() call returns an ID token.

    Firebase Authentication users do not necessarily have access to Google Drive. Instead you'll need to use the user's OAuth2 token (the same one that is used to sign in to Firebase) to access Google Drive. In steps, you'll need to:

    1. request the additional OAuth scopes needed to access Drive,
    2. capture the OAuth token (not a Firebase token) and,
    3. use that to access Drive.

    If you've tried this already, please edit your question to show what you've tried.