Accessing Google Docs from marketplace application after user authentication

I’ve got a Google Apps marketplace app running (started with a basic hello world example). Now I need to figure out how to access Google Docs for users who install my app.

My app is for managing resources and I want it to upload files to the user’s Google Drive and keep track of document references. The tricky part is getting the right permissions to work with their Google Docs after they’ve already logged into my marketplace application.

I know the usual flow involves getting an OAuth token directly from Google’s API, but this feels different since users are already authenticated through the marketplace. How do I bridge that gap to get document access?

Here’s what I’m trying to achieve:

def upload_user_document(file_content, filename):
    # Need to authenticate with user's Google Drive here
    drive_service = get_drive_service()  # This is where I'm stuck
    
    file_metadata = {
        'name': filename,
        'parents': ['user_folder_id']
    }
    
    uploaded_file = drive_service.files().create(
        body=file_metadata,
        media_body=file_content
    ).execute()
    
    return uploaded_file.get('id')

Any ideas on the proper authentication flow for marketplace apps?

The problem is Google Workspace Marketplace authentication uses different tokens than Drive API access. I’ve hit this exact issue before - you need a hybrid approach. When users install your marketplace app, they get an installation token for basic marketplace stuff. But for Drive operations, you need a separate OAuth2 flow requesting Drive scopes specifically. The tricky bit is connecting these two authentication contexts. In your get_drive_service() function, check if the user already granted Drive permissions and stored those credentials. If not, redirect them to complete the Drive OAuth flow. Once you have Drive credentials, build the service object with googleapiclient.discovery.build() using those tokens. Here’s a gotcha I ran into - handle token refresh properly. Store refresh tokens and implement automatic renewal, or users will get random authentication failures when accessing documents later.

Marketplace authentication doesn’t automatically give you Drive API access - that’s why you’re stuck. You’ll need a separate OAuth flow for Drive API permissions even though users are already logged in through the marketplace. Create a separate auth endpoint in your marketplace app that directs users to Google’s OAuth consent screen for Drive permissions. Use the same Google account that’s already logged into the marketplace for a smoother experience, as Google recognizes them. After obtaining the OAuth token with Drive permissions, store it securely and link it to the user’s marketplace identity. Your get_drive_service() function can then use that token for API calls. Lastly, remember to account for token refresh, since Drive tokens expire. In essence, marketplace auth and Drive API auth are two distinct systems that you must connect in your app logic.

yo! make sure ya add the drive scope to your oauth request. the marketplace auth only gives basic profile access - for drive API ya need https://www.googleapis.com/auth/drive.file scope. just include that in your oauth flow n users will see the drive permission when they consent.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.