Recent

Author Topic: Upload to YouTube app  (Read 2376 times)

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Upload to YouTube app
« on: January 17, 2018, 01:41:07 pm »
Hi,

Can you advice, how to write an app to upload video to YouTube with Google authorization - a window which appears to ask your's login and password.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Upload to YouTube app
« Reply #1 on: January 17, 2018, 08:33:49 pm »
The Google API package has Youtube service implemented. I haven't tested it myself, but below is a (relatively) simple example on how to use the package for Google Cloud Storage service:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   googleservice,
  5.   googleclient,
  6.   googlestorage,
  7.   openssl,
  8.   jsonparser,
  9.   fpjson,
  10.   fpoauth2,
  11.   fpwebclient,
  12.   fphttpwebclient;
  13.  
  14. type
  15.   TAuthHelper = class
  16.     procedure DoUserConsent(const AURL: String; out AAuthCode: String);
  17.   end;
  18.  
  19. procedure TAuthHelper.DoUserConsent(const AURL: String; out AAuthCode: String);
  20. begin
  21.   WriteLn('Open the following URL in browser:');
  22.   WriteLn(AURL);
  23.   Write('Writeback the resulting code here: ');
  24.   ReadLn(AAuthCode);
  25. end;
  26.  
  27. var
  28.   AuthHelper: TAuthHelper;
  29.   GClient: TGoogleClient;
  30.   GStorageAPI: TStorageAPI;
  31.   BucketsResource: TBucketsResource;
  32.   BucketsListOptions: TBucketsListOptions;
  33.   Buckets: TBuckets;
  34.   i: Integer;
  35. begin
  36.   try
  37.     // Register Tasks resources.
  38.     TStorageAPI.RegisterAPIResources;
  39.  
  40.     // Set up google client.
  41.     GClient := TGoogleClient.Create(nil);
  42.     GClient.WebClient := TFPHTTPWebClient.Create(nil);
  43.     GClient.WebClient.RequestSigner := GClient.AuthHandler;
  44.     GClient.WebClient.LogFile := 'requests.log';
  45.     GClient.AuthHandler.WebClient := GClient.WebClient;
  46.     GClient.AuthHandler.Config.AccessType := atOffLine;
  47.  
  48.     // We want to enter a code.
  49.     AuthHelper := TAuthHelper.Create;
  50.     GClient.OnUserConsent := @AuthHelper.DoUserConsent;
  51.  
  52.     // Create a Tasks API and connect it to the client.
  53.     GStorageAPI := TStorageAPI.Create(nil);
  54.     GStorageAPI.GoogleClient := GClient;
  55.  
  56.     // Registered application needs tasks scope
  57.     GClient.AuthHandler.Config.ClientID := '<my client id>';
  58.     GClient.AuthHandler.Config.ClientSecret := '<my client secret>';
  59.     GClient.AuthHandler.Config.AuthScope := 'https://www.googleapis.com/auth/devstorage.read_only';
  60.     // We are offline.
  61.     GClient.AuthHandler.Config.RedirectUri := 'urn:ietf:wg:oauth:2.0:oob';
  62.     // Session data
  63.     GClient.AuthHandler.Session.RefreshToken := '';
  64.     GClient.AuthHandler.Session.AccessToken := '';
  65.     GClient.AuthHandler.Session.AuthTokenType := '';
  66.     GClient.AuthHandler.Session.AuthExpires := 0;
  67.     GClient.AuthHandler.Session.AuthExpiryPeriod := 0;
  68.  
  69.     BucketsResource := GStorageAPI.CreateBucketsResource;
  70.     BucketsListOptions.Project := '<my project number>';
  71.     Buckets := BucketsResource.List(BucketsListOptions);
  72.     if Assigned(Buckets) then
  73.       for i := 0 to Length(Buckets.Items) - 1 do
  74.         WriteLn(Buckets.Items[i].name + ': ' + Buckets.Items[i].selfLink)
  75.     else
  76.       WriteLn('Failed to list bucket');
  77.   finally
  78.     AuthHelper.Free;
  79.     GStorageAPI.Free;
  80.     GClient.Free;
  81.   end;
  82. end.
  83.  
Youtube shouldn't differ much as the package is structured as such (just assign client to the created service instance). It's kind of jungle without documentation as the units are generated instead of hand coded. Sometimes you have to google to find out the correct API, then find matching implementation. In your case, I believe it would be this API which is implemented by TVideosResource.Insert (use Ctrl+F to find the class). Note that regarding authentication the code only provides OnUserConsent callback, it's up to you how to open the URL and assign AAuthCode. In above case, URL will be simply printed, user must copy that to browser to get the code then copy the code back. The authentication protocol provides several ways, but this one is the simplest. I left other ways for you to find out.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: Upload to YouTube app
« Reply #2 on: January 18, 2018, 04:34:05 pm »
Thanks for your advice and example. I'll explore it.

 

TinyPortal © 2005-2018