YouTube API v3 在尝试列出我的订阅时抛出 "No filter selected" 错误
YouTube API v3 throws "No filter selected" error trying to list my subscriptions
我正在尝试检索包含我所有 subscriptions 的列表,但是,我在调用 [=15= 时收到此错误消息的异常] 在下面的源代码中:
Google.Apis.Requests.RequestError No filter selected. Expected one of:
mySubscribers, id, mine, myRecentSubscribers, channelId [400] Errors [
Message[No filter selected. Expected one of: mySubscribers, id, mine,
myRecentSubscribers, channelId] Location[ - parameter]
Reason[missingRequiredParameter] Domain[youtube.parameter] ]
我读了这个,但我完全不明白:
以及官方参考:
我正在使用 YouTubeService.Scope.Youtube
范围 (https://www.googleapis.com/auth/youtube)
我正在使用“snippet
”列表选择器尝试列出我的订阅,我也尝试使用“id,snippet,contentDetails
”加入。
我用来验证的自动生成的 client_secrets.json 文件包含以下内容:
{
"installed":{
"client_id":"xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"project_id":"xxxxxxxxxxxxxxxxxxx",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"xxxxxxxxxxxxxxxxx",
"redirect_uris":[
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
我上面提供的信息有问题吗?,以及我该如何解决这个问题?
这里是相关的源代码:
请注意,如果我将 属性 listRequest.Mine
设置为 True
,则它不会抛出异常,但会发现零订阅。
C#(在线翻译):
public Task<List<Subscription>> GetSubscriptions(SubscriptionsResource.ListRequest.OrderEnum resultSorting)
{
if (!(this.isAuthorizedB)) {
throw new InvalidOperationException(this.authExceptionMessage);
} else {
SubscriptionsResource.ListRequest listRequest = Await this.client.Subscriptions.List("snippet");
// listRequest.MySubscribers = False
// listRequest.Mine = False
listRequest.MaxResults = 50;
listRequest.Order = resultSorting;
SubscriptionListResponse searchResponse = Await listRequest.ExecuteAsync();
List<Subscription> subscriptions = new List<Subscription>();
while (true) {
foreach (Subscription searchResult in searchResponse.Items) {
subscriptions.Add(searchResult);
}
if (!string.IsNullOrEmpty(searchResponse.NextPageToken)) {
listRequest.PageToken = searchResponse.NextPageToken;
searchResponse = listRequest.ExecuteAsync();
} else {
break; // TODO: might not be correct. Was : Exit Do
}
}
return subscriptions;
}
}
VB.NET(原件):
Public Async Function GetSubscriptions(ByVal resultSorting As SubscriptionsResource.ListRequest.OrderEnum) As Task(Of List(Of Subscription))
If Not (Me.isAuthorizedB) Then
Throw New InvalidOperationException(Me.authExceptionMessage)
Else
Dim listRequest As SubscriptionsResource.ListRequest = Me.client.Subscriptions.List("snippet")
With listRequest
' .MySubscribers = False
' .Mine = False
.MaxResults = 50
.Order = resultSorting
End With
Dim searchResponse As SubscriptionListResponse = Await listRequest.ExecuteAsync()
Dim subscriptions As New List(Of Subscription)
Do While True
For Each searchResult As Subscription In searchResponse.Items
subscriptions.Add(searchResult)
Next searchResult
If Not String.IsNullOrEmpty(searchResponse.NextPageToken) Then
listRequest.PageToken = searchResponse.NextPageToken
searchResponse = Await listRequest.ExecuteAsync()
Else
Exit Do
End If
Loop
Return subscriptions
End If
End Function
好吧,期待评论。注意:YouTube API 与其他 Google API 不同,它可能有点奇怪。
当您的代码 运行 是身份验证部分时
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
它将弹出并请求访问您的帐户。然后它会询问哪个 YouTube 帐户
当你运行你的列表
SubscriptionsResource.ListRequest listRequest = Await this.client.Subscriptions.List("snippet");
listRequest.Mine = true
listRequest.MaxResults = 50;
listRequest.Order = resultSorting;
SubscriptionListResponse searchResponse = Await listRequest.ExecuteAsync();
它只会显示您选择的 YouTube 帐户的结果。因此,您需要做的第一件事是确保您使用正确的 Google 帐户和正确的 YouTube 帐户进行身份验证。您当前选择的帐户没有订阅。
您将需要对每个通过电子邮件进行身份验证的帐户进行身份验证。您还没有发布您的身份验证代码,但在我的代码中,我只是更改了 "userName",它将请求不同用户的访问权限。它只是一个字符串,您可以发送任何您想要识别不同用户的内容。如果您不更改字符串,则 FileDatastore 已经有一个用户,并且它知道这一点,因此不会再次请求访问权限。我关于文件数据存储的教程 Google .net FileDatastore Demystified。所以改变它,它会再次请求访问:)
我还有一个 YouTube 示例项目,我已经有一段时间没有玩过它了,但它可能会有所帮助。 Google-dotnet-samples YouTube github
我正在尝试检索包含我所有 subscriptions 的列表,但是,我在调用 [=15= 时收到此错误消息的异常] 在下面的源代码中:
Google.Apis.Requests.RequestError No filter selected. Expected one of: mySubscribers, id, mine, myRecentSubscribers, channelId [400] Errors [ Message[No filter selected. Expected one of: mySubscribers, id, mine, myRecentSubscribers, channelId] Location[ - parameter] Reason[missingRequiredParameter] Domain[youtube.parameter] ]
我读了这个,但我完全不明白:
以及官方参考:
我正在使用 YouTubeService.Scope.Youtube
范围 (https://www.googleapis.com/auth/youtube)
我正在使用“snippet
”列表选择器尝试列出我的订阅,我也尝试使用“id,snippet,contentDetails
”加入。
我用来验证的自动生成的 client_secrets.json 文件包含以下内容:
{
"installed":{
"client_id":"xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"project_id":"xxxxxxxxxxxxxxxxxxx",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"xxxxxxxxxxxxxxxxx",
"redirect_uris":[
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
我上面提供的信息有问题吗?,以及我该如何解决这个问题?
这里是相关的源代码:
请注意,如果我将 属性 listRequest.Mine
设置为 True
,则它不会抛出异常,但会发现零订阅。
C#(在线翻译):
public Task<List<Subscription>> GetSubscriptions(SubscriptionsResource.ListRequest.OrderEnum resultSorting)
{
if (!(this.isAuthorizedB)) {
throw new InvalidOperationException(this.authExceptionMessage);
} else {
SubscriptionsResource.ListRequest listRequest = Await this.client.Subscriptions.List("snippet");
// listRequest.MySubscribers = False
// listRequest.Mine = False
listRequest.MaxResults = 50;
listRequest.Order = resultSorting;
SubscriptionListResponse searchResponse = Await listRequest.ExecuteAsync();
List<Subscription> subscriptions = new List<Subscription>();
while (true) {
foreach (Subscription searchResult in searchResponse.Items) {
subscriptions.Add(searchResult);
}
if (!string.IsNullOrEmpty(searchResponse.NextPageToken)) {
listRequest.PageToken = searchResponse.NextPageToken;
searchResponse = listRequest.ExecuteAsync();
} else {
break; // TODO: might not be correct. Was : Exit Do
}
}
return subscriptions;
}
}
VB.NET(原件):
Public Async Function GetSubscriptions(ByVal resultSorting As SubscriptionsResource.ListRequest.OrderEnum) As Task(Of List(Of Subscription))
If Not (Me.isAuthorizedB) Then
Throw New InvalidOperationException(Me.authExceptionMessage)
Else
Dim listRequest As SubscriptionsResource.ListRequest = Me.client.Subscriptions.List("snippet")
With listRequest
' .MySubscribers = False
' .Mine = False
.MaxResults = 50
.Order = resultSorting
End With
Dim searchResponse As SubscriptionListResponse = Await listRequest.ExecuteAsync()
Dim subscriptions As New List(Of Subscription)
Do While True
For Each searchResult As Subscription In searchResponse.Items
subscriptions.Add(searchResult)
Next searchResult
If Not String.IsNullOrEmpty(searchResponse.NextPageToken) Then
listRequest.PageToken = searchResponse.NextPageToken
searchResponse = Await listRequest.ExecuteAsync()
Else
Exit Do
End If
Loop
Return subscriptions
End If
End Function
好吧,期待评论。注意:YouTube API 与其他 Google API 不同,它可能有点奇怪。
当您的代码 运行 是身份验证部分时
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
它将弹出并请求访问您的帐户。然后它会询问哪个 YouTube 帐户
当你运行你的列表
SubscriptionsResource.ListRequest listRequest = Await this.client.Subscriptions.List("snippet");
listRequest.Mine = true
listRequest.MaxResults = 50;
listRequest.Order = resultSorting;
SubscriptionListResponse searchResponse = Await listRequest.ExecuteAsync();
它只会显示您选择的 YouTube 帐户的结果。因此,您需要做的第一件事是确保您使用正确的 Google 帐户和正确的 YouTube 帐户进行身份验证。您当前选择的帐户没有订阅。
您将需要对每个通过电子邮件进行身份验证的帐户进行身份验证。您还没有发布您的身份验证代码,但在我的代码中,我只是更改了 "userName",它将请求不同用户的访问权限。它只是一个字符串,您可以发送任何您想要识别不同用户的内容。如果您不更改字符串,则 FileDatastore 已经有一个用户,并且它知道这一点,因此不会再次请求访问权限。我关于文件数据存储的教程 Google .net FileDatastore Demystified。所以改变它,它会再次请求访问:)
我还有一个 YouTube 示例项目,我已经有一段时间没有玩过它了,但它可能会有所帮助。 Google-dotnet-samples YouTube github