/// <summary> /// Adding media to album /// </summary> /// <param name="items"> /// Key .... upload_token (result from UploadFile) /// Value .. item description /// </param> /// <returns></returns> public static GAPINewMediaItemResults AddMediaItemsToAlbum(GAPIAccountConnection conn, string albumId, Dictionary <string, string> items) { try { Logger.Info($"Addding media items to album id {albumId}"); // https://developers.google.com/photos/library/reference/rest/v1/mediaItems/batchCreate var url = "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate"; var newMediaItems = new GAPINewMediaItems(); newMediaItems.albumId = albumId; foreach (var kvp in items) { var newMediaItem = new GLAPINewMediaItem(); newMediaItem.description = kvp.Value; newMediaItem.simpleMediaItem.uploadToken = kvp.Key; newMediaItems.newMediaItems.Add(newMediaItem); } var newMediaItemResults = GAPICommunication.SendRequest <GAPINewMediaItemResults>(url, newMediaItems, conn); return(newMediaItemResults); } catch (Exception ex) { Logger.Error(ex); throw; } }
public static GAPIAlbumsList GetAllAlbums(GAPIAccountConnection conn) { try { var result = new GAPIAlbumsList(); string pageToken = null; do { var albums = GetAlbums(conn, 50, pageToken); result.albums.AddRange(albums.albums); pageToken = albums.nextPageToken; } while (pageToken != null); return(result); } catch (Exception ex) { Logger.Error(ex); throw; } }
public static GAPIAbout AboutUser(GAPIAccountConnection conn) { try { var url = "https://www.googleapis.com/drive/v2/about"; var about = GAPICommunication.SendRequest <GAPIAbout>(url, null, "GET", conn); return(about); } catch (Exception ex) { Logger.Error(ex); throw; } }
public static GAPINewMediaItemResults AddMediaItemToAlbum(GAPIAccountConnection conn, string albumId, string fileName) { Logger.Info($"Addding media item (file {fileName}) to album id {albumId}"); try { var fileToken = conn.UploadFile(fileName); var mediaItems = new Dictionary <string, string>() { { fileToken, fileName } }; return(AddMediaItemsToAlbum(conn, albumId, mediaItems)); } catch (Exception ex) { Logger.Error(ex); throw; } }
public static GAPIAlbum GetAlbum(GAPIAccountConnection conn, string id) { Logger.Info($"Geting album id {id}"); // https://developers.google.com/photos/library/reference/rest/v1/albums/list var idFix = WebUtility.UrlEncode(id); try { var url = $"https://photoslibrary.googleapis.com/v1/albums/{idFix}"; var alb = GAPICommunication.SendRequest <GAPIAlbum>(url, null, "GET", conn); return(alb); } catch (Exception ex) { Logger.Error(ex); throw; } }
public static VoidedPurchasesList GetVoidedPurchases(GAPIAccountConnection conn, string packageName, int pageSize = 20, string pageToken = null) { // https://developers.google.com/android-publisher/voided-purchases?authuser=0 try { var url = $"https://www.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/voidedpurchases?access_token={conn.AccessToken.access_token}"; url += $"?pageSize={pageSize.ToString()}"; if (!String.IsNullOrEmpty(pageToken)) { url += $"&pageToken={pageToken}"; } return(GAPICommunication.SendRequest <VoidedPurchasesList>(url, null, "GET", conn)); } catch (Exception ex) { Logger.Error(ex); throw; } }
private static GAPIAlbumsList GetAlbums(GAPIAccountConnection conn, int pageSize = 20, string pageToken = null) { // https://developers.google.com/photos/library/reference/rest/v1/albums/list try { var url = "https://photoslibrary.googleapis.com/v1/albums"; url += $"?pageSize={pageSize.ToString()}"; if (!String.IsNullOrEmpty(pageToken)) { url += $"&pageToken={pageToken}"; } return(GAPICommunication.SendRequest <GAPIAlbumsList>(url, null, "GET", conn)); } catch (Exception ex) { Logger.Error(ex); throw; } }
public static GAPIAlbum CreateAlbum(GAPIAccountConnection conn, string albumTitle) { Logger.Info($"Creating album {albumTitle}"); // https://developers.google.com/photos/library/reference/rest/v1/albums/create try { var url = $"https://photoslibrary.googleapis.com/v1/albums"; var newAlbum = new GAPINewAlbum(); newAlbum.album.title = albumTitle; var postData = JsonConvert.SerializeObject(newAlbum); return(GAPICommunication.SendRequest <GAPIAlbum>(url, newAlbum, conn)); } catch (Exception ex) { Logger.Error(ex); throw; } }
public static T SendRequest <T>(string url, string data, string method, GAPIAccountConnection conn = null, string contentType = "application/x-www-form-urlencoded") { try { var request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.ContentType = contentType; request.Accept = "application/json"; var responseString = SendRequestBase(request, data, conn); return(JsonConvert.DeserializeObject <T>(responseString)); } catch (Exception ex) { Logger.Error(ex); throw; } }
/// <summary> /// Sends the request. /// </summary> /// <returns>Response string</returns> /// <param name="request">Request.</param> /// <param name="ASCIIPOSTdata">POST Data</param> /// <param name="conn">Initialized GAPIAccountConnection</param> private static string SendRequestBase(HttpWebRequest request, string ASCIIPOSTdata, GAPIAccountConnection conn = null) { try { if (request.Method == "GET") { Logger.Debug($"Sending GET request to url: {request.RequestUri}"); } else { Logger.Debug($"Sending POST request to url: {request.RequestUri}"); if (request.ContentLength >= 0) { Logger.Debug($"Request ContentLength: {request.ContentLength}"); } if (ASCIIPOSTdata != null) { Logger.Debug($"Posting data length: {ASCIIPOSTdata.Length}"); if (request.ContentType != "application/octet-stream") { Logger.Debug($"Posting data: {ASCIIPOSTdata}"); } } } Logger.Debug($"ContentType: {request.ContentType}"); if (conn != null) { if (conn.AccessToken.expires_at < DateTime.Now) { Logger.Info($"Access token expired at [{conn.AccessToken.expires_at.ToString()}]"); conn.RefreshAccessToken(); } var accessToken = WebUtility.UrlEncode(conn.AccessToken.access_token); request.Headers.Add("Authorization", "Bearer " + accessToken); request.PreAuthenticate = true; } if (request.Method == "POST" && ASCIIPOSTdata != null) { // adding post data var postData = string.IsNullOrEmpty(ASCIIPOSTdata) ? new byte[0] : Encoding.ASCII.GetBytes(ASCIIPOSTdata); request.ContentLength = ASCIIPOSTdata.Length; using (var stream = request.GetRequestStream()) { stream.Write(postData, 0, ASCIIPOSTdata.Length); } } request.Timeout = 5 * 60 * 60 * 1000; // 5*60 min timeout Logger.Debug($"Method: {request.Method}"); Logger.Debug($"RequestUri: {request.RequestUri}"); Logger.Debug($"Timeout: {request.Timeout}"); Logger.Debug($"ContentType: {request.ContentType}"); if (request.ContentLength > 0) { Logger.Debug($"ContentLength: {request.ContentLength}"); } foreach (var header in request.Headers) { Logger.Debug($"Header: {header.ToString()}"); } using (var response = (HttpWebResponse)request.GetResponse()) { string responseString; using (var sr = new StreamReader(response.GetResponseStream())) { responseString = sr.ReadToEnd(); } Logger.Debug($"Response: {responseString}"); Logger.Debug($"StatusCode: {response.StatusCode}"); Logger.Debug($"StatusDescription: {response.StatusDescription}"); Logger.Debug($"ContentLength: {response.ContentLength}"); Logger.Debug($"ContentType: {response.ContentType}"); Logger.Debug($"ContentEncoding: {response.ContentEncoding}"); return(responseString); } } catch (Exception ex) { Logger.Error(ex); throw; } }
public static string SendRequest(HttpWebRequest request, GAPIAccountConnection conn = null) { return(SendRequestBase(request, null, conn)); }
public static T SendRequest <T>(string url, GAPIBaseObject obj, GAPIAccountConnection conn) { return(SendRequest <T>(url, JsonConvert.SerializeObject(obj), "POST", conn, "application/json")); }