YouTube API v3引发"未选择过滤器"尝试列出我的订阅时出错

时间:2016-05-30 05:10:04

标签: c# vb.net google-api youtube-api google-api-dotnet-client

我尝试检索包含所有 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

1 个答案:

答案 0 :(得分:3)

好好得到评论。注意: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帐户

enter image description here

运行列表时

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已经有了一个用户,它知道这样就不会再要求访问了。我在filedatastore上的教程Google .net FileDatastore Demystified。所以改变它,它将再次请求访问:)

我还有一个YouTube样本项目,我有一段时间没有玩过,但它可能有所帮助。 Google-dotnet-samples YouTube github