You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Apps Script API调用scripts.run时遇认证凭证缺失问题求助

Fixing "Request is missing required authentication credential" for scripts.run() in Google Apps Script API

I see you're running into an authentication error specifically when calling service.scripts.run(), even though other Apps Script API methods work flawlessly. Let's break down the most probable reasons for this mismatch and how to resolve them.

1. Confirm Your OAuth Scopes Cover the Script's Runtime Needs

Your current scopes handle project management and Drive access, but scripts.run() operates in a user-specific runtime context—so it might need additional permissions depending on what your myFunction does:

  • If your script interacts with other Google services (like Sheets, Docs, or Calendar), you must include the corresponding scopes (e.g., https://www.googleapis.com/auth/spreadsheets for Sheets operations).
  • Even for simple scripts, double-check that your OAuth token actually includes the https://www.googleapis.com/auth/script.scriptapp scope you've defined. You can decode your token's payload (using any local JWT decoder) to verify the scopes attached to it.

2. Check Service Account Impersonation (If Applicable)

If you're using a service account for authentication:

  • Domain-wide delegation must be enabled for the service account in your Google Cloud Console.
  • You need to specify a user to impersonate when creating your auth client. Methods like projects.create work without impersonation because they're tied to the Cloud project itself, but scripts.run() executes on behalf of a specific user and requires this context.

Here's how to adjust your service account auth setup:

const { google } = require('googleapis');
const auth = new google.auth.GoogleAuth({
  keyFile: 'path/to/your-service-account-key.json',
  scopes: [
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/script.projects',
    'https://www.googleapis.com/auth/script.scriptapp',
    // Add any extra scopes your script requires here
  ],
  subject: 'user@yourdomain.com' // Replace with the user to impersonate
});
const service = google.script({ version: 'v1', auth });

3. Validate devMode Configuration

You've set devMode: true in your request, which runs the latest draft of your script instead of a deployed version. Ensure:

  • The authenticated (or impersonated) user has edit access to the Apps Script project—dev mode requires permission to access and execute draft code.
  • The draft version of your script has any necessary external service permissions enabled (if it calls APIs outside of Apps Script).

4. Refresh Your OAuth Token

Stale or incomplete tokens can sometimes cause this error, even if your code looks correct. Try regenerating your auth token:

  • For user-based OAuth: Re-run the authorization flow to make sure all requested scopes are granted by the user.
  • For service accounts: Regenerate your service account key file and reinitialize the auth client with the new key.

Debugging Step

To confirm your token has the right scopes, add this debug log before calling scripts.run():

const tokenDetails = await auth.getAccessToken();
console.log('Active token scopes:', tokenDetails.res.data.scope);

This will print all scopes attached to your current token, so you can verify that every required scope is present.

内容的提问来源于stack exchange,提问作者jnewbie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 10:01:45