Пример #1
0
        private static async Task EnumerateObjects(string project)
        {
            var auth  = new GoogleOAuth2("https://www.googleapis.com/auth/devstorage.read_write");
            var token = await auth.Authenticate("");

            var defaults = new DynamicRestClientDefaults()
            {
                AuthScheme = "OAuth",
                AuthToken  = token
            };

            dynamic google         = new DynamicRestClient("https://www.googleapis.com/", defaults);
            dynamic bucketEndPoint = google.storage.v1.b;
            dynamic buckets        = await bucketEndPoint.get(project : project);

            foreach (var bucket in buckets.items)
            {
                Console.WriteLine("bucket {0}: {1}", bucket.id, bucket.name);

                dynamic contents = await bucketEndPoint(bucket.id).o.get();

                foreach (var item in contents.items)
                {
                    Console.WriteLine("\tid: {0}", item.id);
                    Console.WriteLine("\tname: {0}", item.name);
                    Console.WriteLine("\tcontent type: {0}", item.contentType);
                    Console.WriteLine("\t-----");
                }
            }
        }
Пример #2
0
 public YouTube(OAuth2Info oauth)
 {
     googleAuth = new GoogleOAuth2(oauth, this)
     {
         Scope = "https://www.googleapis.com/auth/youtube.upload"
     };
 }
Пример #3
0
        public async Task MultiPartUploadObject()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/devstorage.read_write");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var defaults = new DynamicRestClientDefaults()
            {
                AuthScheme = "OAuth",
                AuthToken  = _token
            };

            dynamic google = new DynamicRestClient("https://www.googleapis.com/", defaults);

            using (var stream = new StreamInfo(File.OpenRead(@"D:\temp\test2.png"), "image/png"))
            {
                dynamic metaData = new ExpandoObject();
                metaData.name = "test2";
                dynamic result = await google.upload.storage.v1.b.unit_tests.o.post(metaData, stream, uploadType : new PostUrlParam("multipart"));

                Assert.IsNotNull(result);
            }
        }
Пример #4
0
        // [Ignore] // - this test requires user interaction
        public async Task CreateCalendar()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            using (var client = new HttpClient(MockInitialization.Handler, false))
            {
                client.BaseAddress = new Uri("https://www.googleapis.com/calendar/v3/");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _token);

                using (dynamic proxy = new DynamicRestClient(client))
                {
                    dynamic calendar = new ExpandoObject();
                    calendar.summary = "unit_testing";

                    var list = await proxy.calendars.post(calendar);

                    Assert.IsNotNull(list);
                    Assert.AreEqual("unit_testing", list.summary);
                }
            }
        }
Пример #5
0
 public GoogleDrive(OAuth2Info oauth)
 {
     GoogleAuth = new GoogleOAuth2(oauth, this)
     {
         Scope = "https://www.googleapis.com/auth/drive"
     };
 }
Пример #6
0
        //  [Ignore] // - this test requires user interaction
        public async Task DeleteCalendar()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var client = new RestClient("https://www.googleapis.com/calendar/v3");

            client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(_token);
            dynamic proxy = new RestSharpProxy(client);
            var     list  = await proxy.users.me.calendarList.get();

            Assert.IsNotNull(list);

            string id = ((IEnumerable <dynamic>)(list.items)).Where(cal => cal.summary == "unit_testing").Select(cal => (string)cal.id).FirstOrDefault();

            Assert.IsFalse(string.IsNullOrEmpty(id));

            var result = await proxy.calendars(id).delete();

            Assert.IsNull(result);

            //list = await proxy.users.me.calendarList.get();
            //Assert.IsNotNull(list);
            //id = ((IEnumerable<dynamic>)(list.items)).Where(cal => cal.summary == "unit_testing").Select(cal => (string)cal.id).FirstOrDefault();

            //Assert.IsTrue(string.IsNullOrEmpty(id));
        }
Пример #7
0
 public GooglePhotos(OAuth2Info oauth)
 {
     GoogleAuth = new GoogleOAuth2(oauth, this)
     {
         Scope = "https://www.googleapis.com/auth/photoslibrary https://www.googleapis.com/auth/photoslibrary.sharing"
     };
 }
Пример #8
0
 public GoogleCloudStorage(OAuth2Info oauth)
 {
     googleAuth = new GoogleOAuth2(oauth, this)
     {
         Scope = "https://www.googleapis.com/auth/devstorage.read_write"
     };
 }
Пример #9
0
        public void CancelPassesToConfigurationCallback()
        {
            using (var source = new CancellationTokenSource())
            {
                // the cancellation token here is the one we passed in below
                using (dynamic client = new DynamicRestClient("https://www.googleapis.com/oauth2/v1/userinfo", null,
                                                              async(request, cancelToken) =>
                {
                    Assert.AreEqual(source.Token, cancelToken);

                    var oauth = new GoogleOAuth2("email profile");
                    var authToken = await oauth.Authenticate("", cancelToken);
                    request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authToken);
                }))
                {
                    // run request on a different thread and do not await thread
                    Task t = client.oauth2.v1.userinfo.get(source.Token);

                    // cancel on unit test thread
                    source.Cancel();

                    try
                    {
                        // this will throw
                        Task.WaitAll(t);
                        Assert.Fail("Task was not cancelled");
                    }
                    catch (AggregateException e)
                    {
                        Assert.IsTrue(e.InnerExceptions.OfType <TaskCanceledException>().Any());
                    }
                }
            }
        }
Пример #10
0
        // [Ignore] // - this test requires user interaction
        public async Task CreateCalendar()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var handler = new HttpClientHandler();

            if (handler.SupportsAutomaticDecompression)
            {
                handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }

            using (var client = new HttpClient(handler, true))
            {
                client.BaseAddress = new Uri("https://www.googleapis.com/calendar/v3/");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _token);

                dynamic proxy = new HttpClientProxy(client);

                dynamic calendar = new ExpandoObject();
                calendar.summary = "unit_testing";

                var list = await proxy.calendars.post(calendar);

                Assert.IsNotNull(list);
                Assert.AreEqual("unit_testing", (string)list.summary);
            }
        }
Пример #11
0
        //  [Ignore] // - this test requires user interaction
        public async Task GetUserProfile()
        {
            var auth = new GoogleOAuth2("email profile");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");
            var handler = new HttpClientHandler();

            if (handler.SupportsAutomaticDecompression)
            {
                handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }

            using (var client = new HttpClient(handler, true))
            {
                client.BaseAddress = new Uri("https://www.googleapis.com");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _token);

                dynamic proxy   = new HttpClientProxy(client);
                var     profile = await proxy.oauth2.v1.userinfo.get();

                Assert.IsNotNull(profile);
                Assert.AreEqual("Kackman", (string)profile.family_name);
            }
        }
Пример #12
0
 public GooglePhotos(OAuth2Info oauth)
 {
     GoogleAuth = new GoogleOAuth2(oauth, this)
     {
         Scope = "https://picasaweb.google.com/data"
     };
 }
Пример #13
0
 public YouTube(OAuth2Info oauth)
 {
     GoogleAuth = new GoogleOAuth2(oauth, this)
     {
         Scope = "youtube.upload"
     };
 }
Пример #14
0
        //  [Ignore] // - this test requires user interaction
        public async Task DeleteCalendar()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            using (var client = new HttpClient(MockInitialization.Handler, false))
            {
                client.BaseAddress = new Uri("https://www.googleapis.com/calendar/v3/");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _token);

                using (dynamic proxy = new DynamicRestClient(client))
                {
                    var list = await proxy.users.me.calendarList.get();

                    Assert.IsNotNull(list);

                    string id = ((IEnumerable <dynamic>)(list.items)).Where(cal => cal.summary == "unit_testing").Select(cal => cal.id).FirstOrDefault();
                    Assert.IsFalse(string.IsNullOrEmpty(id), "calendar does not exist so we can't delete it");

                    var result = await proxy.calendars(id).delete();

                    Assert.IsNull(result);

                    //var list2 = await proxy.users.me.calendarList.get();
                    //Assert.IsNotNull(list2);
                    //var id2 = ((IEnumerable<dynamic>)(list2.items)).Where(cal => cal.summary == "unit_testing").Select(cal => (string)cal.id).FirstOrDefault();

                    //Assert.IsTrue(string.IsNullOrEmpty(id2), "calendar seems to have not been deleted");
                }
            }
        }
Пример #15
0
 public GoogleURLShortener(AccountType uploadMethod, string anonymousKey, OAuth2Info oauth)
 {
     UploadMethod = uploadMethod;
     AnonymousKey = anonymousKey;
     GoogleAuth   = new GoogleOAuth2(oauth, this)
     {
         Scope = "https://www.googleapis.com/auth/urlshortener"
     };
 }
Пример #16
0
        public static void SetUseCloudSaves(bool useCloud)
        {
            GoogleOAuth2.KillPendingAuthorization();

            var settingsDB = PlayerSettingsDB.Get();

            settingsDB.useCloudStorage = useCloud;

            if (useCloud && CloudStorage != null)
            {
                if (!cloudSettingsInitialized)
                {
                    CloudStorageInit();
                }
                else
                {
                    CloudStorageSendNotifies(CloudSaveState.UpToDate);
                }
            }

            lock (cloudSettingsSyncLock)
            {
                cloudSettingsCanSave = useCloud;
            }

            if (cloudSettingsUpdateTask == null)
            {
                cloudSettingsUpdateTask = new Task(async() =>
                {
                    const int intervalMs = 2 * 60 * 1000;
                    while (true)
                    {
                        await Task.Delay(intervalMs);

                        bool canSave = false;
                        lock (cloudSettingsSyncLock)
                        {
                            canSave = cloudSettingsCanSave;
                        }

                        if (canSave)
                        {
                            if (PlayerSettingsDB.Get().isDirty)
                            {
                                await CloudStorageSave();
                            }
                            else
                            {
                                CloudStorageSendNotifies(CloudSaveState.UpToDate);
                            }
                        }
                    }
                });
                cloudSettingsUpdateTask.Start();
            }
        }
Пример #17
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    GoogleOAuth2.GoogleAuthToken(Request, Response);

                    Response.Redirect("~/MemberPages/CreateCheckin.aspx");
                }
                catch (Exception ex)
                {
                }
            }
        }
Пример #18
0
        //  [Ignore] // - this test requires user interaction
        public async Task GetCalendarList()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var client = new RestClient("https://www.googleapis.com/calendar/v3");

            client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(_token);
            dynamic proxy = new RestSharpProxy(client);
            var     list  = await proxy.users.me.calendarList.get();

            Assert.IsNotNull(list);
            Assert.AreEqual("calendar#calendarList", (string)list.kind);
        }
Пример #19
0
        //  [Ignore] // - this test requires user interaction
        public async Task GetUserProfile()
        {
            var auth = new GoogleOAuth2("profile");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var client = new RestClient("https://www.googleapis.com");

            client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(_token);
            dynamic proxy   = new RestSharpProxy(client);
            var     profile = await proxy.oauth2.v1.userinfo.get();

            Assert.IsNotNull(profile);
            Assert.AreEqual("Kackman", (string)profile.family_name);
        }
Пример #20
0
        public async Task AuthenticateAndGetUserName()
        {
            dynamic google = new DynamicRestClient("https://www.googleapis.com/", null, async(request, cancelToken) =>
            {
                // this demonstrates how t use the configuration callback to handle authentication
                var auth  = new GoogleOAuth2("email profile");
                var token = await auth.Authenticate("", cancelToken);
                Assert.IsNotNull(token, "auth failed");

                request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", token);
            });

            var profile = await google.oauth2.v1.userinfo.get();

            Assert.IsNotNull(profile);
            Assert.AreEqual("Kackman", (string)profile.family_name);
        }
Пример #21
0
 protected void Page_Load(object sender, EventArgs e)
 {
     if (GoogleOAuth2.IsGoogleAuthenticated(Page.Request)) // logged in
     {
         LoginButton.Text   = "Google Log Off";
         LoginButton.Click -= LogOffGoogle;
         LoginButton.Click -= LogInGoogle;
         LoginButton.Click += LogOffGoogle;
     }
     else // logged off
     {
         LoginButton.Text   = "Google Log In";
         LoginButton.Click -= LogInGoogle;
         LoginButton.Click -= LogOffGoogle;
         LoginButton.Click += LogInGoogle;
     }
 }
Пример #22
0
        //  [Ignore] // - this test requires user interaction
        public async Task UpdateCalendar()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var handler = new HttpClientHandler();

            if (handler.SupportsAutomaticDecompression)
            {
                handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }

            using (var client = new HttpClient(handler, true))
            {
                client.BaseAddress = new Uri("https://www.googleapis.com/calendar/v3/");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _token);
                dynamic proxy = new HttpClientProxy(client);
                var     list  = await proxy.users.me.calendarList.get();

                Assert.IsNotNull(list);

                string id = ((IEnumerable <dynamic>)(list.items)).Where(cal => cal.summary == "unit_testing").Select(cal => (string)cal.id).FirstOrDefault();
                Assert.IsFalse(string.IsNullOrEmpty(id));

                var     guid     = Guid.NewGuid().ToString();
                dynamic calendar = new ExpandoObject();
                calendar.summary     = "unit_testing";
                calendar.description = guid;

                var result = await proxy.calendars(id).put(calendar);

                Assert.IsNotNull(result);

                list = await proxy.users.me.calendarList.get();

                Assert.IsNotNull(list);
                string description = ((IEnumerable <dynamic>)(list.items)).Where(cal => cal.summary == "unit_testing").Select(cal => (string)cal.description).FirstOrDefault();

                Assert.AreEqual(guid, description);
            }
        }
Пример #23
0
        public async Task UploadString()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/devstorage.read_write");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var defaults = new DynamicRestClientDefaults()
            {
                AuthScheme = "OAuth",
                AuthToken  = _token
            };

            dynamic google = new DynamicRestClient("https://www.googleapis.com/", defaults);

            dynamic result = await google.upload.storage.v1.b.unit_tests.o.post("text", name : new PostUrlParam("string_object"), uploadType : new PostUrlParam("media"));

            Assert.IsNotNull(result);
        }
Пример #24
0
        [Ignore] // drive scopes don't work with device pin based oauth2 - ignore for now
        public async Task UploadFile()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/devstorage.read_write");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var defaults = new DynamicRestClientDefaults()
            {
                AuthScheme = "OAuth",
                AuthToken  = _token
            };

            dynamic google = new DynamicRestClient("https://www.googleapis.com/", defaults);

            dynamic result = await google.upload.drive.v2.files.post(File.OpenRead(@"D:\temp\test.png"), uploadType : "media", title : "unit_test.jpg");

            Assert.IsNotNull(result);
        }
Пример #25
0
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.viewModel.Name = "getting...";
                var googleAuth = new GoogleOAuth2(this.Output);
                await googleAuth.PerformAuthenticationAsync(() => this.Activate());

                Output("*** Done auth. Making an API call now. ***");
                Output(string.Empty);

                var googleApis   = new ApiCalls(this.Output);
                var responseJson = await googleApis.UserinfoCallAsync(googleAuth);

                this.UpdateContactInfo(responseJson);
            }
            catch (Exception ex)
            {
                Output(ex.Message);
            }
        }
Пример #26
0
        // [Ignore] // - this test requires user interaction
        public async Task CreateCalendar()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var client = new RestClient("https://www.googleapis.com/calendar/v3");

            client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(_token);
            dynamic proxy = new RestSharpProxy(client);

            dynamic calendar = new ExpandoObject();

            calendar.summary = "unit_testing";

            var list = await proxy.calendars.post(calendar);

            Assert.IsNotNull(list);
            Assert.AreEqual("unit_testing", (string)list.summary);
        }
Пример #27
0
        //  [Ignore] // - this test requires user interaction
        public async Task GetUserProfile()
        {
            var auth = new GoogleOAuth2("email profile");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            using (var client = new HttpClient(MockInitialization.Handler, false))
            {
                client.BaseAddress = new Uri("https://www.googleapis.com");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _token);

                using (dynamic proxy = new DynamicRestClient(client))
                {
                    var profile = await proxy.oauth2.v1.userinfo.get();

                    Assert.IsNotNull(profile);
                    Assert.AreEqual("Kackman", profile.family_name);
                }
            }
        }
Пример #28
0
        //  [Ignore] // - this test requires user interaction
        public async Task GetCalendarList()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            using (var client = new HttpClient(MockInitialization.Handler, false))
            {
                client.BaseAddress = new Uri("https://www.googleapis.com/calendar/v3/");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _token);

                using (dynamic proxy = new DynamicRestClient(client))
                {
                    var list = await proxy.users.me.calendarList.get();

                    Assert.IsNotNull(list);
                    Assert.AreEqual("calendar#calendarList", list.kind);
                }
            }
        }
Пример #29
0
        public static string InitTreeView(string accessToken, string refreshToken)
        {
            try
            {
                // Make the Auth request to Google
                SpreadsheetsService sheetsService = GoogleOAuth2.GoogleAuthSheets(accessToken, refreshToken);
                if (sheetsService == null)
                {
                    return("");
                }

                // Get list of sheets
                DriveService driveService = GoogleOAuth2.GoogleAuthDrive(accessToken, refreshToken);
                if (driveService == null)
                {
                    return("");
                }
                List <GoogleSheet> sheetList = GoogleDriveHelpers.GoogleRetrieveAllSheets(sheetsService);
                GoogleFolder       root      = GoogleDriveHelpers.GoogleRetrieveSheetTree(driveService, null, ref sheetList);

                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(root.GetType());
                //using (var stream = new StreamWriter(@"", false))
                //{
                //    x.Serialize(stream, root);
                //}

                string xmlStr = GoogleDriveHelpers.SerializeXml <GoogleFolder>(root);
                xmlStr = xmlStr.Replace("<Children>", "").Replace("<Children />", "").Replace("</Children>", "");
                xmlStr = xmlStr.Replace("<Sheets>", "").Replace("<Sheets />", "").Replace("</Sheets>", "");

                return(xmlStr);
            }
            catch (Exception ex)
            {
                return("");
            }
        }
Пример #30
0
        //  [Ignore] // - this test requires user interaction
        public async Task UpdateCalendar()
        {
            var auth = new GoogleOAuth2("email profile https://www.googleapis.com/auth/calendar");

            _token = await auth.Authenticate(_token);

            Assert.IsNotNull(_token, "auth failed");

            var client = new RestClient("https://www.googleapis.com/calendar/v3");

            client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(_token);
            dynamic proxy = new RestSharpProxy(client);
            var     list  = await proxy.users.me.calendarList.get();

            Assert.IsNotNull(list);

            string id = ((IEnumerable <dynamic>)(list.items)).Where(cal => cal.summary == "unit_testing").Select(cal => (string)cal.id).FirstOrDefault();

            Assert.IsFalse(string.IsNullOrEmpty(id));

            var     guid     = Guid.NewGuid().ToString();
            dynamic calendar = new ExpandoObject();

            calendar.summary     = "unit_testing";
            calendar.description = guid;

            var result = await proxy.calendars(id).put(calendar);

            Assert.IsNotNull(result);

            list = await proxy.users.me.calendarList.get();

            Assert.IsNotNull(list);
            string description = ((IEnumerable <dynamic>)(list.items)).Where(cal => cal.summary == "unit_testing").Select(cal => (string)cal.description).FirstOrDefault();

            Assert.AreEqual(guid, description);
        }