Bonjour,
Voila plusieurs heures que je creuse le sujet, mais rien n'y fait.
J'utilise Visual Studio 2015 pour un projet WinForm en C#.
Le but de ce projet est de gérer les médias présents dans mon album "Google photos".
Aucun souci concernant l'authentification en Oauth2.
Pas de problème pour récupérer la liste de mes albums existants (je n'ai pas testé d'en créer un, mais cela semble aussi assez simple au vu des méthodes mises à disposition).
Et tout aussi facile de récupérer la liste des médias (photos/vidéos) présents dans un albums sélectionné.
Par contre, je suis incapable d'ajouter un média dans un album ni même dans la librairie parente
Ma question est donc la suivante :
• Quelqu'un aurait-il l'amabilité de m'apporter de l'assistance dans cette entreprise, SVP ?
Quelques précisions tout de même sur le problème rencontré :
1. Je pense avoir compris qu'il est nécessaire de procéder en 2 étapes;
La 1ère, faire un "Uploading bytes" qui permet d'envoyer le fichier vers les serveurs Google et par la même occasion, d'obtenir un "UploadToken" correspondant au fichier importé;
La seconde, effectuer le tant attendu "Creating a media item" qui utilise le "UploadToken" précédemment généré pour affecter le fichier à la librairie de Photos ou à un album spécifié.
Ça, c'est la théorie comprise... dans la pratique, je ne comprends pas comment faire le "Uploading bytes" avec la librairie "Google.Apis.PhotosLibrairie.v1".2. J'utilise les packages NuGet suivants :
• Google.Apis (v1.34.0)
• Google.Apis.Auth (v1.34.0)
• Google.Apis.Core (v1.34.0)
• Google.Apis.PhotosLibrary.v1 (v1.34.0.1223)
• Newtonsoft.Json (v10.0.2)3. Le scope d'authentification utilisé est ".Photoslibrary".
La liste des scopes prévus et leur définition respective se trouvent ICI.4. Mon incompréhension se situe dans le code "Back-end" (c.f.: ci-dessous) et plus exactement dans la méthode nommée "AddPhoto",
où j'ai spécifié via deux régions le code recherché.5. Pour ceux qui ne s'y connaissent pas du tout mais qui aimerait bien testouiller par curiosité, lisez ce super article qui nous met bien l'eau à la bouche ;-)
• Google-photos-library-api-brings-power-google-photos-to-your-app
Pensez bien à activer l'API "Photos Library API" dans votre projet via la "Console Google API".
De même, pensez aussi à créer un nouvel "ID clients OAuth 2.0" (de type autre) spécifique à cette utilisation pour obtenir votre fichier d'authentification "Client_Secret.json".
Je pose ci-dessous mes bouts de codes fonctionnels si ça peut aider à la réflexion![]()
(dsl par avance pour le manque de commentaire)
Ci-dessous le code Back-end :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 using System; using System.IO; using System.Threading.Tasks; using System.Threading; using System.Collections.Generic; using Google.Apis.Services; using Google.Apis.PhotosLibrary.v1; using Google.Apis.PhotosLibrary.v1.Data; using Google.Apis.Auth.OAuth2; namespace TestsPhotoLibrary.Codes.Data { public class GooglePhotosClass { // https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth public delegate void Event(string message); private const string ClientSecretJson = "client_secret.json"; public static Event RunEvent; private async Task<PhotosLibraryService> AuthenticateMeToPhotos() { PhotosLibraryService service; try { UserCredential credential; if (File.Exists(ClientSecretJson)) { using (var stream = new FileStream(ClientSecretJson, FileMode.Open, FileAccess.Read)) { credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, new[] { PhotosLibraryService.Scope.Photoslibrary }, // "https://www.googleapis.com/auth/photoslibrary" "fox15145", CancellationToken.None); } service = new PhotosLibraryService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Photos API Sample", }); } else { RunEvent?.Invoke($"Le fichier \"{ClientSecretJson}\" est introuvable."); service = new PhotosLibraryService(); } } catch (Exception E) { RunEvent?.Invoke(E.Message); throw E; } return service; } private async Task<bool> GetAlbums() { bool WorkFine = false; PhotosLibraryService service = await AuthenticateMeToPhotos(); ListAlbumsResponse cAlbums; RunEvent?.Invoke($"Récupération des albums récents :"); cAlbums = await service.Albums.List().ExecuteAsync(); int Loop_A = 0; foreach (Google.Apis.PhotosLibrary.v1.Data.Album cAlbum in cAlbums.Albums) { RunEvent?.Invoke($"Album N°{Loop_A++.ToString().PadLeft(3, '0')} - {cAlbum.Title}"); RunEvent?.Invoke($" Nb photos : {cAlbum.TotalMediaItems}"); RunEvent?.Invoke($" Id : {cAlbum.Id}"); RunEvent?.Invoke($""); // Mise en commentaire des informations supplémentaires par souci de lecture dans la listbox //RunEvent?.Invoke($" IsWriteable : {((cAlbum.IsWriteable == null) ? "vide" : "illisible ?")}"); //RunEvent?.Invoke($" ETag : {cAlbum.ETag}"); //RunEvent?.Invoke($" ProductUrl : {cAlbum.ProductUrl}"); //RunEvent?.Invoke($" ShareInfo : {cAlbum.ShareInfo}"); //RunEvent?.Invoke($" CoverPhotoBaseUrl : {cAlbum.CoverPhotoBaseUrl}"); if (WorkFine == false) WorkFine = true; } return WorkFine; } public async Task<bool> GetPhotos(string aAlbumId) { bool WorkFine = false; PhotosLibraryService service = await AuthenticateMeToPhotos(); var cPhotos = service.MediaItems.Search(new SearchMediaItemsRequest() { AlbumId = aAlbumId }).ExecuteAsync(); RunEvent?.Invoke($"Récupération des photos de l'album \"{aAlbumId}\" :"); RunEvent?.Invoke($" Nb de photos : {cPhotos.Result.MediaItems.Count}"); int Loop_P = 0; foreach (Google.Apis.PhotosLibrary.v1.Data.MediaItem cPhoto in cPhotos.Result.MediaItems) { // Mise en commentaire des informations supplémentaires par souci de lecture dans la listbox RunEvent?.Invoke($"Photo N°{Loop_P++.ToString().PadLeft(3, '0')} - Id : {cPhoto.Id}"); //RunEvent?.Invoke($" BaseUrl : {cPhoto.BaseUrl}"); //RunEvent?.Invoke($" ContributorInfo : {cPhoto.ContributorInfo}"); //RunEvent?.Invoke($" ETag : {cPhoto.ETag}"); //RunEvent?.Invoke($" ProductUrl : {cPhoto.ProductUrl}"); //RunEvent?.Invoke($" Description : {cPhoto.Description}"); //RunEvent?.Invoke($" MimeType : {cPhoto.MimeType}"); //RunEvent?.Invoke($" MediaMetadata : {cPhoto.MediaMetadata}"); if (WorkFine == false) WorkFine = true; } return WorkFine; } public GooglePhotosClass() { GetAlbums(); } public async Task<string> AddPhoto(string aAlbumId, string PathFilename) { // https://stackoverflow.com/questions/35405901/how-to-post-images-using-plusdoamins-on-google-plus/35407118#35407118 string NewPhotoId = ""; PhotosLibraryService service = await AuthenticateMeToPhotos(); // ************************************* #region "How to do : Uploading Bytes ???" #endregion //***************************************** #region "How to do : Add Media in album ???" #endregion return NewPhotoId; } } }
Ci-dessous le code Front-end qui utilise la classe "GooglePhotosClass" :
Ce post étant, à mon souvenir, mon premier, j'espère qu'il n'est ni trop court ni trop long, mais qu'il réussira a attirer les foules pour y remédier ;-)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public partial class Form1 : Form { Codes.Data.GooglePhotosClass gPhotos; public Form1() { InitializeComponent(); button_ShowAlbums.Text = "Afficher les albums"; button_ShowPhotos.Text = "Afficher les photos"; button_AddMedia.Text = "Ajouter une photo"; button_ShowPhotos.Enabled = false; button_AddMedia.Enabled = false; // par simplicité pour le copier/coller du code button_ShowAlbums.Click += button_ShowAlbums_Click; button_ShowPhotos.Click += button_ShowPhotos_Click; button_AddMedia.Click += button_AddMedia_Click; listBox_Logs.SelectedValueChanged += listBox_Logs_SelectedValueChanged; // Implémentation de l'évènement présent dans les méthodes de la classe "GooglePhotosClass" // Cette méthode permet en l'occurence de récupérer les différents textes renvoyés par ces méthodes. // J'ai choisi ici, d'afficher chaque texte récupéré dans une nouvelle ligne d'un composant visuel listBox. Codes.Data.GooglePhotosClass.RunEvent = delegate (string message) { listBox_Logs.Items.Add(message); }; // Fichier à importer présent dans le répertoire de l'executable textBox_FilenameToUpload.Text = @"MyMediaFile.jpg"; } private void button_ShowAlbums_Click(object sender, EventArgs e) { gPhotos = new Codes.Data.GooglePhotosClass(); button_ShowPhotos.Enabled = false; } private void button_ShowPhotos_Click(object sender, EventArgs e) { gPhotos.GetPhotos(textBox_SelectedItemValue.Text); button_ShowPhotos.Enabled = false; } private void listBox_Logs_SelectedValueChanged(object sender, EventArgs e) { textBox_SelectedItemValue.Text = ""; // Ne récupérer que la "valeur" d'un "ID" de la ligne sélectionnée dans la listbox string cLine = (sender as ListBox).SelectedItem.ToString(); if (cLine.ToLower().IndexOf("id : ") > 0) { textBox_SelectedItemValue.Text = cLine; if (cLine.IndexOf(':') > 0) textBox_SelectedItemValue.Text = cLine.Substring(cLine.IndexOf(':') + 1, cLine.Length - cLine.IndexOf(':') - 1).Trim(); } button_ShowPhotos.Enabled = ((cLine.ToLower().IndexOf("id : ") > 0) && (textBox_SelectedItemValue.Text != string.Empty)); button_ShowPhotos.Enabled = ((cLine.ToLower().IndexOf("id : ") > 0) && (textBox_SelectedItemValue.Text != string.Empty) && (textBox_FilenameToUpload.Text != string.Empty) && (File.Exists(textBox_FilenameToUpload.Text))); } private void button_AddMedia_Click(object sender, EventArgs e) { gPhotos.AddPhoto(textBox_SelectedItemValue.Text, textBox_FilenameToUpload.Text); } }
Merci d'avance
[EDIT : ] Je me suis permis de réécrire les deux zones de codes source, dans un souci de simplicité pour vous lors du copier/coller de ceux-ci.
Je continue de chercher le fonctionnement d'insertion de média via l'API, mais il semblerai que je ne soit pas le seul confronté à cette incompréhension :
• https://stackoverflow.com/questions/...tos-google-com
• https://stackoverflow.com/questions/...gle-photos-api
• https://stackoverflow.com/questions/...tos-google-com
• https://stackoverflow.com/questions/...s-api/32525986 --> L'Api Picasa qui était à priori l'ancienne porte d'entrée est désormais déprécié.
Je reste bien sûr à votre disposition pour toute question à ce sujet permettant de faire avancer le schmilblick ;-)
Bonne journée d'ici là
Partager