Beispiel #1
0
        public void RefreshAccessToken()
        {
            try
            {
                Logger.Info("Refreshing access token");

                // https://developers.google.com/identity/protocols/OAuth2InstalledApp

                var refreshTokenFix = WebUtility.UrlEncode(AccessToken.refresh_token);

                var url = "https://www.googleapis.com/oauth2/v4/token";

                string postData = $"refresh_token={AccessToken.refresh_token_urlencoded}";
                postData += $"&client_id={AuthInfo.client_id}";
                postData += $"&client_secret={AuthInfo.client_secret}";
                postData += $"&grant_type=refresh_token";

                var refreshedAccessToken = GAPICommunication.SendRequest <GAPIAccessToken>(url, postData, "POST");

                AccessToken.access_token = refreshedAccessToken.access_token;
                AccessToken.expires_at   = DateTime.Now.AddSeconds(Convert.ToDouble(refreshedAccessToken.expires_in));

                AccessToken.SaveToFile("token.json");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }
Beispiel #2
0
        public void ReceiveAccessToken(string code)
        {
            try
            {
                Logger.Info("Receiving access token");

                // https://developers.google.com/identity/protocols/OAuth2InstalledApp

                var redirectURI = "urn:ietf:wg:oauth:2.0:oob";
                code = WebUtility.UrlEncode(code);

                var url = "https://www.googleapis.com/oauth2/v4/token";

                string postData = $"code={code}";
                postData += $"&client_id={AuthInfo.client_id}";
                postData += $"&client_secret={AuthInfo.client_secret}";
                postData += $"&redirect_uri={redirectURI}";
                postData += $"&grant_type=authorization_code";

                AccessToken            = GAPICommunication.SendRequest <GAPIAccessToken>(url, postData, "POST", null);
                AccessToken.expires_at = DateTime.Now.AddSeconds(Convert.ToDouble(AccessToken.expires_in));

                AccessToken.SaveToFile("token.json");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }
Beispiel #3
0
        /// <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;
            }
        }
Beispiel #4
0
        public string UploadFile(string fileName)
        {
            try
            {
                Logger.Info($"Uploading file {fileName}");

                // https://developers.google.com/photos/library/guides/upload-media

                var url = "https://photoslibrary.googleapis.com/v1/uploads";

                var request = (HttpWebRequest)WebRequest.Create(url);

                request.Method      = "POST";
                request.ContentType = "application/octet-stream";

                var fNameValidForHeader = GAPICommunication.NormalizeStringForUrl(Path.GetFileNameWithoutExtension(fileName)) +
                                          "." + GAPICommunication.NormalizeStringForUrl(Path.GetExtension(fileName));
                request.Headers["X-Goog-Upload-File-Name"] = fNameValidForHeader; // non ascii characters cause "System.ArgumentException: Specified value has invalid CRLF characters."
                request.Headers["X-Goog-Upload-Protocol"]  = "raw";

                // fill request stream buffer with file
                var    bytesRead = 0;
                byte[] buffer    = new byte[16384];

                string responseString;

                using (var requestStream = request.GetRequestStream())
                {
                    using (var fs = System.IO.File.OpenRead(fileName))
                    {
                        while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            requestStream.Write(buffer, 0, bytesRead);
                        }
                    }

                    Logger.Debug("Posting file data");

                    responseString = GAPICommunication.SendRequest(request, this);
                }

                return(responseString);
            } catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }
Beispiel #5
0
        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;
            }
        }
Beispiel #6
0
        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;
            }
        }
Beispiel #8
0
        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;
            }
        }
Beispiel #9
0
        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;
            }
        }