How to redirect Google Sheets to a function within Google Sheets

I am working on redirecting a URL to a function in Google Sheets. This function is supposed to perform some tasks later on. As far as I understand, I need to set up an API for this function by using ‘Deploy as web app’ and then utilize that URL.

Thus, I’ve created two script files. The first one is named Api.gs where I have this code:

function sampleFunction() {
  Logger.log('Operation successful!')
}

This is what I plan to make the API for. The second file is Authentication.gs which contains this function:

function displaySidebar() {
    var authUrl = 'https://www.upwork.com/ab/account-security/oauth2/authorize?redirect_uri=YOUR_API_URL_HERE&client_id=yourClientID&response_type=token';
    var template = HtmlService.createTemplate(
        '<a href="<?= authUrl ?>" target="_blank">Click here to authorize</a>');
    template.authUrl = authUrl;
    var page = template.evaluate().setTitle('Authorization');
    SpreadsheetApp.getUi().showSidebar(page);
}

This function creates a sidebar, and when I click the link, it redirects me properly but shows an error message that states Missing or invalid redirect_uri parameter value.

I am attempting to authenticate with Upwork and then redirect back to Google Sheets. Is there something wrong with my approach, or is this even feasible?

yeah, oauth providers need a real web endpoint - they can’t talk to sheets directly. when you deploy as a web app, set execution to ‘anyone’ and access to ‘anyone with link’. that’s where most people mess up. your doGet function needs something like function doGet(e) { return HtmlService.createHtmlOutput('auth complete'); } to handle the callback.

Google Sheets can’t work as an OAuth redirect endpoint - that’s your problem. When you deploy Apps Script as a web app, you get a URL that handles HTTP requests, but OAuth providers like Upwork need specific redirect formats that Sheets doesn’t support.

Here’s what you need to do: deploy your Apps Script as a web app and use that URL as your redirect_uri. Don’t try redirecting back to the Sheets interface itself. In your deployed web app, create a doGet() function that handles the OAuth callback, processes the authorization code, and stores the auth data in your spreadsheet.

I’ve done similar OAuth flows before. The standard approach is handling the entire OAuth process server-side in your web app, then updating your spreadsheet data programmatically. You don’t want users redirected back to the Sheets interface anyway - this keeps things secure and stops those redirect_uri validation errors.

You’re using the wrong URL type for your redirect_uri. You can’t redirect straight to Google Sheets - OAuth needs a proper web endpoint to catch the callback. Deploy your Apps Script as a web app and make sure you’ve got a doGet(e) function in your code. This function grabs the OAuth response and pulls out the authorization code or token from the URL parameters. Replace ‘YOUR_API_URL_HERE’ with your actual web app URL. I’ve dealt with similar API auth flows before. Your web app acts as the middleman - it catches the OAuth response, handles the credentials, then shows a success message or talks to your spreadsheet. Just make sure your redirect_uri matches exactly what you registered in Upwork’s developer console, including the protocol and any trailing slashes.