如何实现嵌入式youtube订阅按钮android?

时间:2016-09-01 16:20:06

标签: android youtube-api

如何在我的Android应用中添加嵌入式YouTube订阅按钮?我需要一个按钮,将它们订阅到我的频道,但是像网页按钮一样留在我的应用中,但对于Android。我尝试了一个订阅链接,但它将它们从我的应用程序中删除。

1 个答案:

答案 0 :(得分:1)

使用Youtube Data API中的subscriptions.insert为经过身份验证的用户的频道添加订阅。按下按钮时,执行这部分代码。

代码段:

try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "addsubscription");

        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
                "youtube-cmdline-addsubscription-sample").build();

        // We get the user selected channel to subscribe.
        // Retrieve the channel ID that the user is subscribing to.
        String channelId = getChannelId();
        System.out.println("You chose " + channelId + " to subscribe.");

        // Create a resourceId that identifies the channel ID.
        ResourceId resourceId = new ResourceId();
        resourceId.setChannelId(channelId);
        resourceId.setKind("youtube#channel");

        // Create a snippet that contains the resourceId.
        SubscriptionSnippet snippet = new SubscriptionSnippet();
        snippet.setResourceId(resourceId);

        // Create a request to add the subscription and send the request.
        // The request identifies subscription metadata to insert as well
        // as information that the API server should return in its response.
        Subscription subscription = new Subscription();
        subscription.setSnippet(snippet);
        YouTube.Subscriptions.Insert subscriptionInsert =
                youtube.subscriptions().insert("snippet,contentDetails", subscription);
        Subscription returnedSubscription = subscriptionInsert.execute();

        // Print information from the API response.
        System.out.println("\n================== Returned Subscription ==================\n");
        System.out.println("  - Id: " + returnedSubscription.getId());
        System.out.println("  - Title: " + returnedSubscription.getSnippet().getTitle());

    }

这里有一个相关的SO thread供额外参考。