Exemple #1
0
 public bool CheckAuthorization()
 {
     return(GoogleAuth.CheckAuthorization());
 }
Exemple #2
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            if (!CheckAuthorization())
            {
                return(null);
            }

            UploadResult result = new UploadResult();

            if (IsPublic)
            {
                AlbumID = CreateAlbum(fileName).id;

                GooglePhotosAlbumOptions albumOptions = new GooglePhotosAlbumOptions();

                string serializedAlbumOptions         = JsonConvert.SerializeObject(albumOptions);
                string serializedAlbumOptionsResponse = SendRequest(HttpMethod.POST, $"https://photoslibrary.googleapis.com/v1/albums/{AlbumID}:share", content: serializedAlbumOptions, headers: GoogleAuth.GetAuthHeaders(), contentType: UploadHelpers.ContentTypeJSON);
                GooglePhotosAlbumOptionsResponse albumOptionsResponse = JsonConvert.DeserializeObject <GooglePhotosAlbumOptionsResponse>(serializedAlbumOptionsResponse);

                result.URL = albumOptionsResponse.shareInfo.shareableUrl;
            }

            NameValueCollection uploadTokenHeaders = new NameValueCollection
            {
                { "X-Goog-Upload-File-Name", fileName },
                { "X-Goog-Upload-Protocol", "raw" },
                { "Authorization", GoogleAuth.GetAuthHeaders()["Authorization"] }
            };

            string uploadToken = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/uploads", stream, contentType: UploadHelpers.ContentTypeOctetStream, headers: uploadTokenHeaders);

            GooglePhotosNewMediaItemRequest newMediaItemRequest = new GooglePhotosNewMediaItemRequest
            {
                albumId       = AlbumID,
                newMediaItems = new GooglePhotosNewMediaItem[]
                {
                    new  GooglePhotosNewMediaItem
                    {
                        simpleMediaItem = new GooglePhotosSimpleMediaItem
                        {
                            uploadToken = uploadToken
                        }
                    }
                }
            };

            string serializedNewMediaItemRequest = JsonConvert.SerializeObject(newMediaItemRequest);

            result.Response = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", serializedNewMediaItemRequest, headers: GoogleAuth.GetAuthHeaders(), contentType: UploadHelpers.ContentTypeJSON);

            GooglePhotosNewMediaItemResults newMediaItemResult = JsonConvert.DeserializeObject <GooglePhotosNewMediaItemResults>(result.Response);

            if (!IsPublic)
            {
                result.URL = newMediaItemResult.newMediaItemResults[0].mediaItem.productUrl;
            }

            return(result);
        }
Exemple #3
0
 public bool RefreshAccessToken()
 {
     return(GoogleAuth.RefreshAccessToken());
 }
Exemple #4
0
        public GooglePhotosAlbum CreateAlbum(string albumName)
        {
            GooglePhotosNewAlbum newItemAlbum = new GooglePhotosNewAlbum
            {
                album = new GooglePhotosAlbum
                {
                    title = albumName
                }
            };

            string serializedNewItemAlbum         = JsonConvert.SerializeObject(newItemAlbum);
            string serializedNewItemAlbumResponse = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/albums", serializedNewItemAlbum, headers: GoogleAuth.GetAuthHeaders(), contentType: UploadHelpers.ContentTypeJSON);

            GooglePhotosAlbum newItemAlbumResponse = JsonConvert.DeserializeObject <GooglePhotosAlbum>(serializedNewItemAlbumResponse);

            return(newItemAlbumResponse);
        }
Exemple #5
0
        public List <GooglePhotosAlbumInfo> GetAlbumList()
        {
            if (!CheckAuthorization())
            {
                return(null);
            }

            List <GooglePhotosAlbumInfo> albumList = new List <GooglePhotosAlbumInfo>();

            Dictionary <string, string> args = new Dictionary <string, string>
            {
                { "excludeNonAppCreatedData", "true" }
            };

            string pageToken = "";

            do
            {
                args["pageToken"] = pageToken;
                string response = SendRequest(HttpMethod.GET, "https://photoslibrary.googleapis.com/v1/albums", args, headers: GoogleAuth.GetAuthHeaders());
                pageToken = "";

                if (!string.IsNullOrEmpty(response))
                {
                    GooglePhotosAlbums albums = JsonConvert.DeserializeObject <GooglePhotosAlbums>(response);

                    if (albums.albums != null)
                    {
                        foreach (GooglePhotosAlbum album in albums.albums)
                        {
                            GooglePhotosAlbumInfo AlbumInfo = new GooglePhotosAlbumInfo
                            {
                                ID   = album.id,
                                Name = album.title
                            };

                            albumList.Add(AlbumInfo);
                        }
                        pageToken = albums.nextPageToken;
                    }
                }
            }while (!string.IsNullOrEmpty(pageToken));

            return(albumList);
        }
Exemple #6
0
        public List <GoogleDriveFile> GetFolders(bool trashed = false, bool writer = true)
        {
            if (!CheckAuthorization())
            {
                return(null);
            }

            string query = "mimeType = 'application/vnd.google-apps.folder'";

            if (!trashed)
            {
                query += " and trashed = false";
            }

            if (writer)
            {
                query += " and 'me' in writers";
            }

            Dictionary <string, string> args = new Dictionary <string, string>();

            args.Add("q", query);
            args.Add("fields", "nextPageToken,files(id,name,description)");

            List <GoogleDriveFile> folders = new List <GoogleDriveFile>();
            string pageToken = "";

            // Make sure we get all the pages of results
            do
            {
                args["pageToken"] = pageToken;
                string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v3/files", args, GoogleAuth.GetAuthHeaders());
                pageToken = "";

                if (!string.IsNullOrEmpty(response))
                {
                    GoogleDriveFileList fileList = JsonConvert.DeserializeObject <GoogleDriveFileList>(response);

                    if (fileList != null)
                    {
                        folders.AddRange(fileList.files);
                        pageToken = fileList.nextPageToken;
                    }
                }
            }while (!string.IsNullOrEmpty(pageToken));

            return(folders);
        }
Exemple #7
0
 public UserBuilder WithGoogleAuth(GoogleAuth googleAuth)
 {
     this.googleAuth = googleAuth;
     return(this);
 }
Exemple #8
0
 public bool GetAccessToken(string code)
 {
     return(GoogleAuth.GetAccessToken(code));
 }
Exemple #9
0
        private void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, bool allowFileDiscovery)
        {
            if (!CheckAuthorization())
            {
                return;
            }

            string url = string.Format("https://www.googleapis.com/drive/v3/files/{0}/permissions", fileID);

            string json = JsonConvert.SerializeObject(new
            {
                role = role.ToString(),
                type = type.ToString(),
                allowFileDiscovery = allowFileDiscovery.ToString()
            });

            string response = SendRequest(HttpMethod.POST, url, json, UploadHelpers.ContentTypeJSON, null, GoogleAuth.GetAuthHeaders());
        }
Exemple #10
0
        public GooglePhotosAlbum CreateAlbum(string albumName)
        {
            GooglePhotosNewAlbum newItemAlbum = new GooglePhotosNewAlbum
            {
                album = new GooglePhotosAlbum
                {
                    title = albumName
                }
            };

            Dictionary <string, string> newItemAlbumArgs = new Dictionary <string, string>
            {
                { "fields", "id" }
            };

            string            serializedNewItemAlbum         = JsonConvert.SerializeObject(newItemAlbum);
            string            serializedNewItemAlbumResponse = SendRequest(HttpMethod.POST, "https://photoslibrary.googleapis.com/v1/albums", serializedNewItemAlbum, RequestHelpers.ContentTypeJSON, newItemAlbumArgs, GoogleAuth.GetAuthHeaders());
            GooglePhotosAlbum newItemAlbumResponse           = JsonConvert.DeserializeObject <GooglePhotosAlbum>(serializedNewItemAlbumResponse);

            return(newItemAlbumResponse);
        }
Exemple #11
0
 public string GetAuthorizationURL()
 {
     return(GoogleAuth.GetAuthorizationURL());
 }
Exemple #12
0
 public OAuthUserInfo GetUserInfo()
 {
     return(GoogleAuth.GetUserInfo());
 }
        public List <GoogleDriveSharedDrive> GetDrives()
        {
            if (!CheckAuthorization())
            {
                return(null);
            }

            Dictionary <string, string>   args   = new Dictionary <string, string>();
            List <GoogleDriveSharedDrive> drives = new List <GoogleDriveSharedDrive>();
            string pageToken = "";

            // Make sure we get all the pages of results
            do
            {
                args["pageToken"] = pageToken;
                string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v3/drives", args, GoogleAuth.GetAuthHeaders());
                pageToken = "";

                if (!string.IsNullOrEmpty(response))
                {
                    GoogleDriveSharedDriveList driveList = JsonConvert.DeserializeObject <GoogleDriveSharedDriveList>(response);

                    if (driveList != null)
                    {
                        drives.AddRange(driveList.drives);
                        pageToken = driveList.nextPageToken;
                    }
                }
            }while (!string.IsNullOrEmpty(pageToken));

            return(drives);
        }
        public async void UpdateSubscriber_ValidSubscriber_SubscriberUpdatedInDB()
        {
            _webApp.Load(_webApp.DBContext);

            //setting up login token
            var StringContent            = new StringContent(JsonConvert.SerializeObject(admin), Encoding.UTF8, "application/json");
            HttpResponseMessage response = await _client.PostAsync("api/Admins", StringContent);

            string adminToken = await response.Content.ReadAsStringAsync();

            _client.SetBearerToken(adminToken);

            //get a subscriber form the database
            response = await _client.GetAsync("api/Subscribers");

            //deserialize the retrieved subscribers into a list of Subscriber objects
            string responseJSON = await response.Content.ReadAsStringAsync();

            List <Subscriber> actualSubscribers = JsonConvert.DeserializeObject <List <Subscriber> >(responseJSON);

            //use the first subscriber in the list as the subscriber to update
            //Subscriber UpdateSubscriber = actualSubscribers[100];
            Subscriber UpdateSubscriber = actualSubscribers.FirstOrDefault(sub => sub.email.Equals("*****@*****.**"));

            //update the phone number
            UpdateSubscriber.phoneNumber = "2222222222";

            //Getting valid Google OAuth token
            string googleAuth = GoogleAuth.getGoogleAuth(_client);

            _client.DefaultRequestHeaders.Add("GoogleAuth", googleAuth);

            //Getting subscriber token from api
            response = await _client.GetAsync("api/Subscribers/email-d=" + UpdateSubscriber.email);

            var responseString = await response.Content.ReadAsStringAsync();

            _client.DefaultRequestHeaders.Remove("Authorization");

            _client.DefaultRequestHeaders.Add("Authorization", "Bearer " + responseString);

            //convert the subsciber to a string for posting to the database
            var stringContent = new StringContent(JsonConvert.SerializeObject(UpdateSubscriber), Encoding.UTF8, "application/json");

            response = await _client.PutAsync("api/Subscribers/" + UpdateSubscriber.subscriberID, stringContent);

            //API will send back the update Subscriber object.
            //deserialize that object into a Subscriber object
            responseJSON = await response.Content.ReadAsStringAsync();

            //no content status code is 204
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
            _client.SetBearerToken(adminToken);
            //send a get request with the updated subscriber ID to the database and retrieve the updated subscriber
            response = await _client.GetAsync("api/Subscribers/" + UpdateSubscriber.subscriberID);

            responseJSON = await response.Content.ReadAsStringAsync();

            //deserialize the response into a Subscriber object
            Subscriber GetSubscriber = JsonConvert.DeserializeObject <Subscriber>(responseJSON);


            //check each field of the retrieved Subscriber and make sure it matches each field of the initial subscriber after the phone number was modified with the
            //updated value
            Assert.IsType <int>(UpdateSubscriber.subscriberID);
            Assert.Equal(UpdateSubscriber.firstName, GetSubscriber.firstName);
            Assert.Equal(UpdateSubscriber.lastName, GetSubscriber.lastName);
            //Assert.Equal(UpdateSubscriber.Password, GetSubscriber.Password);
            Assert.Equal(UpdateSubscriber.email, GetSubscriber.email);
            Assert.Equal(UpdateSubscriber.phoneNumber, GetSubscriber.phoneNumber);
            Assert.Equal(UpdateSubscriber.isBusiness, GetSubscriber.isBusiness);
            //Assert.IsType<int>(UpdateSubscriber.locationID);
        }
        public async void AddSubscriber_ValidSubscriber_SubscriberAddedToDB()
        {
            //obtained from google

            string googleAuth = GoogleAuth.getGoogleAuth(_client);

            _client.DefaultRequestHeaders.Add("GoogleAuth", googleAuth);

            //Reload the database.
            //SubscriberFixture.Reload(_webApp.DBContext);
            _webApp.Load(_webApp.DBContext);
            //create a new subscriber to add to the database with valid fields
            //Subscriber NewSubscriber = DatabaseSubscriberFixture.GetDerivedSubscribers()[0];

            Subscriber NewSubscriber = new Subscriber
            {
                firstName   = "John",
                lastName    = "Doe",
                email       = "*****@*****.**",
                phoneNumber = "1234567890",
                isBusiness  = false
            };

            Location location = _webApp.DBContext.Location.First();

            NewSubscriber.locationID = location.locationID;

            //convert the subsciber to a string for posting to the database
            StringContent stringContent = new StringContent(JsonConvert.SerializeObject(NewSubscriber), Encoding.UTF8, "application/json");

            //send a post request to the api with the Subscriber object in JSON form
            var response = await _client.PostAsync("api/Subscribers", stringContent);

            //get the response from the api and read it into a string
            string responseString = await response.Content.ReadAsStringAsync();

            //deserialize the response into a Subscriber object
            Subscriber deserializedSubscriber = JsonConvert.DeserializeObject <Subscriber>(responseString);

            //created status code is 201
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);

            AdminFixture.Load(_webApp.DBContext);

            //setting up login token
            var StringContent = new StringContent(JsonConvert.SerializeObject(admin), Encoding.UTF8, "application/json");

            response = await _client.PostAsync("api/Admins", StringContent);

            string adminToken = await response.Content.ReadAsStringAsync();

            _client.SetBearerToken(adminToken);

            //try to get the added subscriber form the database using the response data from earlier request.
            response = await _client.GetAsync("api/Subscribers/" + deserializedSubscriber.subscriberID);

            //read the response as a string
            responseString = await response.Content.ReadAsStringAsync();

            //deserialize the object retrieved from the database
            Subscriber GetSubscriber = JsonConvert.DeserializeObject <Subscriber>(responseString);

            //check each field of the retrieved Subscriber and ensure it is equal to the subscriber that was initially send in the POST request
            Assert.IsType <int>(deserializedSubscriber.subscriberID);
            Assert.Equal(NewSubscriber.firstName, GetSubscriber.firstName);
            Assert.Equal(NewSubscriber.lastName, GetSubscriber.lastName);
            //Assert.Equal(NewSubscriber.Password, GetSubscriber.Password);
            Assert.Equal(NewSubscriber.email, GetSubscriber.email);
            Assert.Equal(NewSubscriber.phoneNumber, GetSubscriber.phoneNumber);
            Assert.Equal(NewSubscriber.isBusiness, GetSubscriber.isBusiness);
            Assert.IsType <int>(GetSubscriber.locationID);
        }
        void ReleaseDesignerOutlets()
        {
            if (BrowserToggle != null)
            {
                BrowserToggle.Dispose();
                BrowserToggle = null;
            }

            if (FacebookAuth != null)
            {
                FacebookAuth.Dispose();
                FacebookAuth = null;
            }

            if (FatsecretAuth != null)
            {
                FatsecretAuth.Dispose();
                FatsecretAuth = null;
            }

            if (FitbitAuth != null)
            {
                FitbitAuth.Dispose();
                FitbitAuth = null;
            }

            if (FoursquareAuth != null)
            {
                FoursquareAuth.Dispose();
                FoursquareAuth = null;
            }

            if (GoogleAuth != null)
            {
                GoogleAuth.Dispose();
                GoogleAuth = null;
            }

            if (GPS != null)
            {
                GPS.Dispose();
                GPS = null;
            }

            if (LinkedinAuth != null)
            {
                LinkedinAuth.Dispose();
                LinkedinAuth = null;
            }

            if (MioAuth != null)
            {
                MioAuth.Dispose();
                MioAuth = null;
            }

            if (RescuetimeAuth != null)
            {
                RescuetimeAuth.Dispose();
                RescuetimeAuth = null;
            }

            if (ResultsTextView != null)
            {
                ResultsTextView.Dispose();
                ResultsTextView = null;
            }

            if (RunkeeperAuth != null)
            {
                RunkeeperAuth.Dispose();
                RunkeeperAuth = null;
            }

            if (SpotifyAuth != null)
            {
                SpotifyAuth.Dispose();
                SpotifyAuth = null;
            }

            if (TwitterAuth != null)
            {
                TwitterAuth.Dispose();
                TwitterAuth = null;
            }

            if (WithingsAuth != null)
            {
                WithingsAuth.Dispose();
                WithingsAuth = null;
            }
        }