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

Unity调用Google Drive API遇403错误:缺失有效API Key

解决Unity中UnityGoogleDrive获取Drive文件列表时的403 API Key缺失错误

问题描述

UnityGoogleDrive: HTTP/1.1 403 Forbidden
Google Drive API Error Description: Code '403' Message: 'The request is missing a valid API key.'

  • Domain: 'global' Reason: 'forbidden' Message: 'The request is missing a valid API key.' LocationType: '' Location: ''

已获取API Key,但不清楚如何在已解析通用凭证JSON OAuth文件的情况下配置到代码中,当前使用的代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityGoogleDrive;
using UnityGoogleDrive.Data;

public class GoogleDriveManager : MonoBehaviour
{
    [ContextMenu("Copy Template In Drive")]
    public void CopyTemplateFromGoogle()
    {
        GoogleDriveFiles.List().Send().OnDone+= GetAllFiles;
    }

    public void GetAllFiles(FileList fileList)
    {
        foreach (var file in fileList.Files)
        {
            Debug.Log(file.Id);
        }
    }
}

解决方案

方法1:单请求附加API Key

修改CopyTemplateFromGoogle方法,在创建List请求时直接添加API Key参数:

public void CopyTemplateFromGoogle()
{
    var listRequest = GoogleDriveFiles.List();
    // 替换为你的实际API Key
    listRequest.WithParameter("key", "你的API_KEY_HERE");
    listRequest.Send().OnDone += GetAllFiles;
}

方法2:全局配置API Key(推荐)

在脚本初始化阶段统一设置API Key,避免每个请求重复配置:

private void Awake()
{
    // 替换为你的实际API Key
    UnityGoogleDrive.Config.ApiKey = "你的API_KEY_HERE";
}

[ContextMenu("Copy Template In Drive")]
public void CopyTemplateFromGoogle()
{
    GoogleDriveFiles.List().Send().OnDone+= GetAllFiles;
}

注意事项

  • 确认Google Cloud控制台中,你的API Key已启用Google Drive API,否则仍会触发权限错误。
  • 若同时使用OAuth凭证,需保证API Key的权限范围与OAuth配置不冲突,此时优先遵循OAuth的身份验证逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 02:50:59