订阅特定频道后无法获取YouTube频道的更新订阅列表

时间:2020-01-03 13:45:57

标签: android youtube youtube-data-api

嗨,我正在使用youtube数据API并成功实现了用于订阅特定youtube频道的代码并获取用户订阅频道的列表。但是我的问题是当我成功订阅特定的youtube频道之后,我执行了获取列表的代码当时我订阅的频道没有在该列表中获取该频道ID。我没有收到更新的订阅频道列表。

以下订阅YouTube频道的代码

@Override
    protected Subscription doInBackground(Object... params) {
        // code for channel subscribe
        Subscription response = null;

        HashMap<String, String> parameters = new HashMap<>();
        parameters.put("part", "snippet");

        // if you could not able to import the classes then check the dependency in build.gradle
        Subscription subscription = new Subscription();

        SubscriptionSnippet snippet = new SubscriptionSnippet();

        ResourceId resourceId = new ResourceId();

        resourceId.set("channelId", channelId);
        resourceId.set("kind", "youtube#channel");

        snippet.setResourceId(resourceId);
        subscription.setSnippet(snippet);

        YouTube.Subscriptions.Insert subscriptionsInsertRequest = null;
        try {

            subscriptionsInsertRequest = mService.subscriptions().insert(parameters.get("part").toString(), subscription);
            response = subscriptionsInsertRequest.execute();
        } catch (IOException e) {
           /* if you got error message below
            "message" : "Access Not Configured. YouTube Data API has not been used in project YOUR_PROJECT_ID before or it is disabled.
            then goto following link and enable the access
            https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=YOUR_PROJECT_ID*/

            e.printStackTrace();
        }

        return response;
    }

获取用户订阅频道列表的代码如下

@Override
    protected SubscriptionListResponse doInBackground(Object... params) {
        // code for channel subscribe
        SubscriptionListResponse response = null;

        HashMap<String, String> parameters = new HashMap<>();
        parameters.put("part", "snippet");

        YouTube.Subscriptions.List subscriptionsListRequest = null;
        try {
            subscriptionsListRequest = mService.subscriptions().list(parameters.get("part").toString());
            subscriptionsListRequest.setMine(true);
            subscriptionsListRequest.setMaxResults(50L);
            response = subscriptionsListRequest.execute();
        } catch (IOException e) {
           /* if you got error message below
            "message" : "Access Not Configured. YouTube Data API has not been used in project YOUR_PROJECT_ID before or it is disabled.
            then goto following link and enable the access
            https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=YOUR_PROJECT_ID*/

            e.printStackTrace();
        }
        return response;
    }

0 个答案:

没有答案