【问题标题】:YouTube API - The API Key and the authentication credential are from different projectsYouTube API - API 密钥和身份验证凭证来自不同的项目
【发布时间】:2022-01-04 05:01:51
【问题描述】:

我正在尝试使用我的 C# 应用程序从 youtube 获取视频,使用“https://youtube.googleapis.com/youtube/v3/search?forMine=true&order=date&type=video&part=sn-p&key={key}&access_token ={token}" 但我收到此错误:

"error": {
    "code": 400,
    "message": "The API Key and the authentication credential are from different 
projects.",
    "errors": [
      {
        "message": "The API Key and the authentication credential are from 
different projects.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developer console API key",
            "url": 
"https://console.developers.google.com/project/{MyProjectCode}/apiui/credential"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "CONSUMER_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "consumer": "projects/{MyProjectCode}",
          "service": "youtube.googleapis.com"
        }
      }
    ]
  }

但我在https://console.cloud.google.com/apis/api/youtube.googleapis.com/credentials?authuser=1&project={MyProject} 站点上只能看到 1 个 API 密钥和 1 个 OAuth 2.0 客户端 ID(带有客户端 ID 和客户端密码)

您能帮我解决这个问题吗?

代码:

var token = Helper.Decrypt(ytConn.AccessToken);
var url = "https://youtube.googleapis.com/youtube/v3/search? 
forMine=true&order=date&type=video&part=snippet&key=" + _key + 
"&access_token=" + token;

videos = Helper.GetFromAPI<Videos>(url, token);

它这样称呼:

public static T GetFromAPI<T>(string url, string token)
{            
    WebRequest request = WebRequest.Create(url);
    request.Method = "GET";

    ServicePointManager.SecurityProtocol = 
    SecurityProtocolType.Tls12;
    request.Headers.Set("Authorization", "Bearer " + token);                

    request.ContentType = "application/json; charset=utf-8";

    T type = default(T);

    try
    {
        using (WebResponse response = request.GetResponse())
        {
            using (Stream dataStream = response.GetResponseStream())
            {
                using (StreamReader reader = new 
                StreamReader(dataStream))
                {
                    string responseFromServer = reader.ReadToEnd();
                    type = JsonConvert.DeserializeObject<T> 
                    (responseFromServer);
                }
            }
        }
    }
}

提前致谢

【问题讨论】:

  • 请编辑您的问题并包含您的代码。
  • 对不起,哪一个?
  • 哪一个什么?你说你用 C# 做这个,代码在哪里?如果您希望我能够测试您的问题,我需要minimal reproducible example,我需要查看您的代码而不仅仅是端点的 url。我假设您使用的是 Google .net 客户端库。
  • 添加了代码。
  • @DaImTo 我的评论中没有任何冒犯之王,只是认为没有解决方案。谢谢你的回答,我正在测试它

标签: c# youtube-api youtube-data-api


【解决方案1】:

您已添加 API 密钥,访问公共数据需要 API 密钥。他们实际上只需要向 Google 识别您的项目。您还在请求中包含了访问令牌。访问令牌用于访问私人用户数据。访问令牌还可以识别您的项目,所以从技术上讲,您不需要两者。

您收到的错误消息

API Key 和身份验证凭证来自 不同的项目

表示您使用的 API 密钥来自谷歌云控制台上的一个项目,而访问令牌是使用来自谷歌云控制台上不同项目的客户端凭据创建的。虽然您不需要两者,但没有什么能阻止您同时使用其他两者,因为它们需要来自同一个项目。

我认为您只是从代码中删除了关键部分。如果您要发送访问令牌,则不需要密钥。

https://youtube.googleapis.com/youtube/v3/search? 
forMine=true&order=date&type=video&part=snippet + 
"&access_token=" + token;

您应该考虑使用 Google api .net 客户端库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 2019-09-04
    • 2016-01-29
    • 1970-01-01
    • 2020-09-20
    相关资源
    最近更新 更多