google api 身份验证问题或应用程序问题
Issue with authenticating to google api or issue with app
所以,请注意,这是我第一次使用 google 的 API 和开发人员控制台,所以如果我错过了一些经验丰富的 google 开发人员会很明显的事情,请原谅我认为常识。话虽如此。我正在尝试创建一个已安装的应用程序,它将在我的帐户下将视频上传到 youtube。我在 powershell 中编写应用程序,所以我在启动脚本时导入适当的 google .Net 库。从那里开始,我基本上使用了位于此处的示例,并将内容转换为 powershell:
Add-Type -AssemblyName mscorlib
Add-Type -AssemblyName System.Net.Http
Add-Type -AssemblyName System
Add-Type -AssemblyName System.Core
Add-Type -AssemblyName System.Numerics
Add-Type -AssemblyName System.Xml
Add-Type -AssemblyName System.Xml.Linq
Add-Type -AssemblyName System.Data
Add-Type -AssemblyName System.Runtime.Serialization
#the below command imports the following assemblies: Google.Apis.Auth.dll, Google.Apis.Auth.PlatformServices.dll, Google.Apis.Core.dll, Google.Apis.dll, Google.Apis.PlatformServices.dll, Google.Apis.YouTube.v3.dll
Get-ChildItem 'C:\Users\whiggs\Documents\SAPIEN\PowerShell Studio\Projects\youtube\*.dll' | % {[reflection.assembly]::LoadFrom($_.FullName)}
$vid = "C:\Users\whiggs\Documents\gery2.mp4"
#$file = [System.IO.File]::OpenRead("C:\Users\whiggs\Documents\SAPIEN\PowerShell Studio\Projects\youtube\client_id.json")
$filemode = [System.IO.FileMode]::Open
$fileaccess = [System.IO.FileAccess]::Read
$stream = New-object System.IO.FileStream -ArgumentList "C:\Users\whiggs\Documents\SAPIEN\PowerShell Studio\Projects\youtube\client_secret.json", $filemode, $fileaccess
$googlebroker = New-object Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker
$thing = [Google.Apis.Auth.OAuth2.GoogleClientSecrets]::Load($stream)
[string[]]$scope = [Google.Apis.YouTube.v3.YouTubeService+ScopeConstants]::YoutubeUpload
#$scope = [Google.Apis.YouTube.v3.YouTubeService+Scope]::YoutubeUpload
$cancellation = [System.Threading.CancellationToken]::None
$googlebroker = [Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker]::AuthorizeAsync($thing.Secrets, $scope, "<google_username>", $cancellation)
$googlebroker.Wait()
[Google.Apis.Auth.OAuth2.UserCredential]$cred = $googlebroker.Result
$baseclient = new-object Google.Apis.Services.BaseClientService+Initializer
$baseclient.HttpClientInitializer = $cred
$baseclient.ApplicationName = "Contacts Tool"
$service = New-Object Google.Apis.YouTube.v3.YouTubeService($baseclient)
$video = New-Object Google.Apis.YouTube.v3.Data.Video
$video.Snippet = New-Object Google.Apis.YouTube.v3.Data.VideoSnippet
$video.Snippet.Title = "test"
$video.Snippet.Description = "none"
$video.Status = New-Object Google.Apis.YouTube.v3.Data.VideoStatus
$video.Status.PrivacyStatus = "public"
$vidstream = New-Object System.IO.FileStream -ArgumentList $vid, $filemode
$request = $service.Videos.Insert($video, "public", $vidstream, "video/*")
$task = $request.UploadAsync()
$task.Wait()
$vidstream.close()
$vidstream.Dispose()
真的不需要包含代码,因为我知道它是正确编写的,因为没有生成异常。当我 运行 上面的代码时,它 运行s 完成而没有产生异常,但是如果我看一下存储在 $task 中的对象(类型 System.Threading.Tasks.Task),而整体对象报告它 运行 完成,深入挖掘对象的 "Result" 属性 揭示任务实际上失败了,进一步挖掘 "exception" 属性 提供了以下错误消息:
The service youtube has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Access Not Configured. YouTube Data API has not been used in project <snip> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=<snip> then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. [403]
Errors [
Message[Access Not Configured. YouTube Data API has not been used in project <snip> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=<snip> then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.] Location[ - ] Reason[accessNotConfigured] Domain[usageLimits]
]
at Google.Apis.Upload.ResumableUpload`1.<InitiateSessionAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Upload.ResumableUpload.<UploadAsync>d__70.MoveNext()
因此,很明显该应用程序的配置或我对其进行身份验证的方式存在某种问题。但是,我知道该应用程序至少正在接收请求,如您所见 here. So, after doing so research, I have a couple of educated guesses as to what the problem might be, and need some input as to a) which of these (if any) is the actual problem and b) what needs to be done to correct it. My first educated guess involves the parameters I passed to the "AuthorizeAsync" method of the Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker class. According to this 文档:
"In this sample code a new UserCredential instance is created by calling the GoogleWebAuthorizationBroker.AuthorizeAsync method. This static method gets the client secret (or a stream to the client secret), the required scopes, the user identifier, the cancellation token for cancelling an operation, and an optional data store. If the data store is not specified, the default is a FileDataStore with a default Google.Apis.Auth folder. The folder is created in Environment.SpecialFolder.ApplicationData."
我想在上面的声明中重点关注的部分是"the user identifier",因为这是提供的有关此参数描述的所有信息。
我输入的值是一个字符串,其中包含注册该应用程序的 google 帐户的用户名以及上传 youtube 视频的帐户,但我不知道那是否是那个值需要,因为作为此过程的一部分,无论如何我都必须通过网络浏览器登录帐户。如果这确实是问题所在,那么就此参数而言,"the user identifier" 是什么。文档中的更多细节可以大有帮助。关于造成这种情况的原因,我的第二个有根据的猜测与应用程序的配置有关,但更具体地说,与生成的 oauth 凭据有关。该应用程序需要访问的范围显然被认为是敏感的,而且,如果我理解正确,我必须从经过验证的域进行身份验证并配置一堆高级设置,因为有人为我自己而不是为我编写这个项目公司,我只是无权访问。我只想将 YouTube 视频上传到我的帐户,那么为什么我需要从经过验证的域进行身份验证?我该怎么做才能解决这个问题?任何信息都会很棒。
该错误消息仅表示您尚未在 Cloud Console 中启用 API。启用 API 后,您可以 post 收到错误消息吗?此外,请确保您 运行 使用正确项目的凭据连接脚本。您可以 运行 glcoud config list
查看您正在使用的项目。
更正您理解中的错误
一旦用户同意访问您的客户端,"<google_username>"
被 filedatastore 用于存储用户的凭据。如果您想对此有更多了解,那么您应该尝试阅读我在 file datastore
上的教程
$googlebroker = [Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker]::AuthorizeAsync($thing.Secrets, $scope, "<google_username>", $cancellation)
回答您的问题
YouTube Data API has not been used in project before or it is disabled.
表示您尚未在 google Developer console or you have not applied and been granted any quota to this api yet. In Google developer console 的项目中启用 YouTube api 转到 API 库 -> YouTube 数据 API v3 并启用它。完成后单击管理,然后转到配额。如果你以前没有启用它,我怀疑你没有那么现在你将有 0 个配额。
单击阴茎图标并为此申请配额api。可能需要一段时间才能收到回复。
所以,请注意,这是我第一次使用 google 的 API 和开发人员控制台,所以如果我错过了一些经验丰富的 google 开发人员会很明显的事情,请原谅我认为常识。话虽如此。我正在尝试创建一个已安装的应用程序,它将在我的帐户下将视频上传到 youtube。我在 powershell 中编写应用程序,所以我在启动脚本时导入适当的 google .Net 库。从那里开始,我基本上使用了位于此处的示例,并将内容转换为 powershell:
Add-Type -AssemblyName mscorlib
Add-Type -AssemblyName System.Net.Http
Add-Type -AssemblyName System
Add-Type -AssemblyName System.Core
Add-Type -AssemblyName System.Numerics
Add-Type -AssemblyName System.Xml
Add-Type -AssemblyName System.Xml.Linq
Add-Type -AssemblyName System.Data
Add-Type -AssemblyName System.Runtime.Serialization
#the below command imports the following assemblies: Google.Apis.Auth.dll, Google.Apis.Auth.PlatformServices.dll, Google.Apis.Core.dll, Google.Apis.dll, Google.Apis.PlatformServices.dll, Google.Apis.YouTube.v3.dll
Get-ChildItem 'C:\Users\whiggs\Documents\SAPIEN\PowerShell Studio\Projects\youtube\*.dll' | % {[reflection.assembly]::LoadFrom($_.FullName)}
$vid = "C:\Users\whiggs\Documents\gery2.mp4"
#$file = [System.IO.File]::OpenRead("C:\Users\whiggs\Documents\SAPIEN\PowerShell Studio\Projects\youtube\client_id.json")
$filemode = [System.IO.FileMode]::Open
$fileaccess = [System.IO.FileAccess]::Read
$stream = New-object System.IO.FileStream -ArgumentList "C:\Users\whiggs\Documents\SAPIEN\PowerShell Studio\Projects\youtube\client_secret.json", $filemode, $fileaccess
$googlebroker = New-object Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker
$thing = [Google.Apis.Auth.OAuth2.GoogleClientSecrets]::Load($stream)
[string[]]$scope = [Google.Apis.YouTube.v3.YouTubeService+ScopeConstants]::YoutubeUpload
#$scope = [Google.Apis.YouTube.v3.YouTubeService+Scope]::YoutubeUpload
$cancellation = [System.Threading.CancellationToken]::None
$googlebroker = [Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker]::AuthorizeAsync($thing.Secrets, $scope, "<google_username>", $cancellation)
$googlebroker.Wait()
[Google.Apis.Auth.OAuth2.UserCredential]$cred = $googlebroker.Result
$baseclient = new-object Google.Apis.Services.BaseClientService+Initializer
$baseclient.HttpClientInitializer = $cred
$baseclient.ApplicationName = "Contacts Tool"
$service = New-Object Google.Apis.YouTube.v3.YouTubeService($baseclient)
$video = New-Object Google.Apis.YouTube.v3.Data.Video
$video.Snippet = New-Object Google.Apis.YouTube.v3.Data.VideoSnippet
$video.Snippet.Title = "test"
$video.Snippet.Description = "none"
$video.Status = New-Object Google.Apis.YouTube.v3.Data.VideoStatus
$video.Status.PrivacyStatus = "public"
$vidstream = New-Object System.IO.FileStream -ArgumentList $vid, $filemode
$request = $service.Videos.Insert($video, "public", $vidstream, "video/*")
$task = $request.UploadAsync()
$task.Wait()
$vidstream.close()
$vidstream.Dispose()
真的不需要包含代码,因为我知道它是正确编写的,因为没有生成异常。当我 运行 上面的代码时,它 运行s 完成而没有产生异常,但是如果我看一下存储在 $task 中的对象(类型 System.Threading.Tasks.Task),而整体对象报告它 运行 完成,深入挖掘对象的 "Result" 属性 揭示任务实际上失败了,进一步挖掘 "exception" 属性 提供了以下错误消息:
The service youtube has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Access Not Configured. YouTube Data API has not been used in project <snip> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=<snip> then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. [403]
Errors [
Message[Access Not Configured. YouTube Data API has not been used in project <snip> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=<snip> then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.] Location[ - ] Reason[accessNotConfigured] Domain[usageLimits]
]
at Google.Apis.Upload.ResumableUpload`1.<InitiateSessionAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Upload.ResumableUpload.<UploadAsync>d__70.MoveNext()
因此,很明显该应用程序的配置或我对其进行身份验证的方式存在某种问题。但是,我知道该应用程序至少正在接收请求,如您所见 here. So, after doing so research, I have a couple of educated guesses as to what the problem might be, and need some input as to a) which of these (if any) is the actual problem and b) what needs to be done to correct it. My first educated guess involves the parameters I passed to the "AuthorizeAsync" method of the Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker class. According to this 文档: "In this sample code a new UserCredential instance is created by calling the GoogleWebAuthorizationBroker.AuthorizeAsync method. This static method gets the client secret (or a stream to the client secret), the required scopes, the user identifier, the cancellation token for cancelling an operation, and an optional data store. If the data store is not specified, the default is a FileDataStore with a default Google.Apis.Auth folder. The folder is created in Environment.SpecialFolder.ApplicationData."
我想在上面的声明中重点关注的部分是"the user identifier",因为这是提供的有关此参数描述的所有信息。 我输入的值是一个字符串,其中包含注册该应用程序的 google 帐户的用户名以及上传 youtube 视频的帐户,但我不知道那是否是那个值需要,因为作为此过程的一部分,无论如何我都必须通过网络浏览器登录帐户。如果这确实是问题所在,那么就此参数而言,"the user identifier" 是什么。文档中的更多细节可以大有帮助。关于造成这种情况的原因,我的第二个有根据的猜测与应用程序的配置有关,但更具体地说,与生成的 oauth 凭据有关。该应用程序需要访问的范围显然被认为是敏感的,而且,如果我理解正确,我必须从经过验证的域进行身份验证并配置一堆高级设置,因为有人为我自己而不是为我编写这个项目公司,我只是无权访问。我只想将 YouTube 视频上传到我的帐户,那么为什么我需要从经过验证的域进行身份验证?我该怎么做才能解决这个问题?任何信息都会很棒。
该错误消息仅表示您尚未在 Cloud Console 中启用 API。启用 API 后,您可以 post 收到错误消息吗?此外,请确保您 运行 使用正确项目的凭据连接脚本。您可以 运行 glcoud config list
查看您正在使用的项目。
更正您理解中的错误
一旦用户同意访问您的客户端,"<google_username>"
被 filedatastore 用于存储用户的凭据。如果您想对此有更多了解,那么您应该尝试阅读我在 file datastore
$googlebroker = [Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker]::AuthorizeAsync($thing.Secrets, $scope, "<google_username>", $cancellation)
回答您的问题
YouTube Data API has not been used in project before or it is disabled.
表示您尚未在 google Developer console or you have not applied and been granted any quota to this api yet. In Google developer console 的项目中启用 YouTube api 转到 API 库 -> YouTube 数据 API v3 并启用它。完成后单击管理,然后转到配额。如果你以前没有启用它,我怀疑你没有那么现在你将有 0 个配额。
单击阴茎图标并为此申请配额api。可能需要一段时间才能收到回复。