7

Well I am able to upload video on Youtube but i didn't find a way or relevant code to delete video/videos from Youtube.

Here is my code which i tried to delete the youtube video.

private async Task Run()
    {
      UserCredential credential;
      using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
      {
        credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            new[] { YouTubeService.Scope.Youtube },
            "user",
            CancellationToken.None
        );
      }
      var youtubeService = new YouTubeService(new BaseClientService.Initializer()
      {
        HttpClientInitializer = credential,
        ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
      });

      var videosDeleteRequest = youtubeService.Videos.Delete("Video ID");
      await videosDeleteRequest.ExecuteAsync();
    }

But getting 403 response

Error: Google.Apis.Requests.RequestError
Insufficient Permission [403]
Errors [
        Message[Insufficient Permission] Location[ - ] Reason[insufficientPermis
sions] Domain[global]
]

A little help or any possible solution will be highly appreciable.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Jyotish Singh
  • 2,065
  • 3
  • 20
  • 28
  • your authentication looks correct and that is also the correct scope are you sure user / channel you authenticated with has that video id? try doing a videos list first to make sure. – Linda Lawton - DaImTo Sep 16 '15 at 06:37
  • We are able to upload video using same connection but when we try to get list of videos or delete video then this connection doesn't work. It gives us 403 response. – Jyotish Singh Sep 16 '15 at 07:01
  • Thanks @DaImTo for your help. Now we are able to delete videos using google delete API with access token. – Jyotish Singh Sep 16 '15 at 10:58
  • What was the problem? – Linda Lawton - DaImTo Sep 16 '15 at 11:01
  • To delete a video, we need as below. API : https://www.googleapis.com/youtube/v3/videos?id = **VideoId** Host : www.googleapis.com Authorization : Bearer **AccessToken** Here we need only one thing which is AccessToken, Which we could get from above connection object("credential") like string accToken = credential.Token.AccessToken; After that simply call **Delete** method using c# will delete the video. – Jyotish Singh Sep 16 '15 at 11:32
  • 1
    @JyotishSingh how you use that bearer token in above code in question, i just can't figure it out? – m.qayyum Jul 08 '16 at 22:06

2 Answers2

0

The error translates to:

The video that you are trying to delete cannot be deleted. The request might not be properly authorized.

https://developers.google.com/youtube/v3/docs/videos/delete

Have you successfully acquired the token of the user that owns the video?

Joe
  • 677
  • 1
  • 6
  • 13
0

The videos.delete method is preformed on private user data. In order to delete the data you must have permission or consent of the user to access their account. They must have granted you permission in one of the following scopes.

enter image description here

The error message

Message[Insufficient Permission] Location[ - ] Reason[insufficientPermis
sions] Domain[global]

Means that the user did not grant you permission with a hig enough scope. If for example you asked for authorization with only a read only scope you would then not have enough permissions to delete a video.

However if we check your code we can see that you are in fact using YouTubeService.Scope.Youtube. However if you have previously run your application then the client library stored the consent of the user. If you then have changed the scope and not forced the user to consent to authorization again. Then you are still running on the old consent.

The solution in this case is to change "user" to something else which will force it to request authorization again.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449