【发布时间】: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