【发布时间】:2016-09-27 19:55:16
【问题描述】:
我正在尝试检索包含我所有 subscriptions 的列表,但是,在下面的源代码中调用 Await listRequest.ExecuteAsync() 时,我收到了一条异常消息:
Google.Apis.Requests.RequestError 未选择过滤器。预期之一: mySubscribers, id, mine, myRecentSubscribers, channelId [400] 错误 [ 消息[未选择过滤器。预期之一:mySubscribers, id, mine, myRecentSubscribers, channelId] 位置[-参数] 原因[missingRequiredParameter] 域[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 帐户附加到您的 Google 帐户。当您进行身份验证时,它会弹出并要求您选择一个 YouTube 帐户。你选对了吗?
-
您需要发送 channelId、id、mine 或 mySubscriptions 过滤器。如果没有其中之一,您将收到 No filter selected 错误。
-
@DaImTo 感谢您的评论。我不确定您是否理解,看,我有两个不同的 Google 帐户,Youtube 将这些帐户识别为两个不同的帐户(不是“附加”),它显示主要登录帐户和“其他帐户:... XXX”。实际上
GoogleWebAuthorizationBroker.AuthorizeAsync()方法没有显示任何身份验证弹出窗口,我将要检索订阅的帐户的电子邮件传递给该方法,它通过了一次身份验证并且不再显示弹出窗口。我在尝试列出我上传的视频时遇到了同样的问题。
标签: c# vb.net google-api youtube-api google-api-dotnet-client