public async Task Subscribe(string channelId) { if (string.IsNullOrEmpty(channelId)) { return; } try { var resourcId = new ResourceId() { ChannelId = channelId, Kind = "youtube#channel" }; var snippet = new SubscriptionSnippet() { ResourceId = resourcId }; var body = new Subscription() { Snippet = snippet }; var request = _youTubeService.GetAuthorizedService().Subscriptions.Insert(body, "snippet"); request.Key = _youTubeService.ApiKey; var response = await request.ExecuteAsync(); Add(channelId, response.Id); } catch (Exception e) { Debug.WriteLine(e); } }
private async void SubscribeButton_Click(object sender, RoutedEventArgs e) { UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = "957928808020-pa0lopl3crh565k6jd4djaj36rm1d9i5.apps.googleusercontent.com", ClientSecret = "oB9U6yWFndnBqLKIRSA0nYGm" }, new[] { YouTubeService.Scope.Youtube }, "user", System.Threading.CancellationToken.None); // Create the service. var service = new YouTubeService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Youtube Viewer", }); if (isSubscribed == true) { SubscribeButton.Content = "Subscribe " + YoutubeMethodsStatic.ViewCountShortner(channel.Statistics.SubscriberCount); var getSubscription = service.Subscriptions.List("snippet"); getSubscription.Mine = true; var subscriptions = await getSubscription.ExecuteAsync(); Subscription subscription = new Subscription(); Constants.MainPageRef.LoadSubscriptions(); try { var sub = Constants.MainPageRef.subscriptionsList.Single(x => x.Id == Constants.activeChannelID); try { var unsubscribe = service.Subscriptions.Delete(sub.SubscriptionID); await unsubscribe.ExecuteAsync(); isSubscribed = false; } catch { //Fires if subscription could not be removed for whatever reason. SubscribeButton.Content = "Subscribed " + YoutubeMethodsStatic.ViewCountShortner(channel.Statistics.SubscriberCount + 1); } } catch { //Fires if subscription doesn't exist. SubscribeButton.Content = "Subscribe " + YoutubeMethodsStatic.ViewCountShortner(channel.Statistics.SubscriberCount); isSubscribed = false; } } else { Subscription subscription = new Subscription(); SubscriptionSnippet snippet = new SubscriptionSnippet(); ResourceId resourceId = new ResourceId { ChannelId = Constants.activeChannelID, Kind = "youtube#channel" }; snippet.ResourceId = resourceId; subscription.Snippet = snippet; var subscribe = service.Subscriptions.Insert(subscription, "snippet"); subscribe.Execute(); SubscribeButton.Content = "Subscribed " + YoutubeMethodsStatic.ViewCountShortner(channel.Statistics.SubscriberCount + 1); isSubscribed = true; } }