Exemplo n.º 1
0
        public void TestReadFromFile()
        {
            var credential = WebAppCredential.ReadFromFile(@"OAuth2\sample-web-app.json");

            Assert.Equal(OAuth2CredentialType.WebApp, credential.AppType);
            Assert.Equal("246-qwert.apps.googleusercontent.com", credential.ClientId);
            Assert.Equal("manychois-googleapis", credential.ProjectId);
            Assert.Equal("https://accounts.google.com/o/oauth2/auth", credential.AuthUri);
            Assert.Equal("https://accounts.google.com/o/oauth2/token", credential.TokenUri);
            Assert.Equal("https://www.googleapis.com/oauth2/v1/certs", credential.AuthProviderX509CertUrl);
            Assert.Equal("abcde12345", credential.ClientSecret);
            Assert.Equal(new string[] { "http://localhost/oauth2callback" }, credential.RedirectUrls);
            Assert.Equal(new string[] { "http://localhost" }, credential.JavaScriptOrigins);
        }
        private void ObtainValidAccessToken(TestConfig testConfig)
        {
            lock (_lock)
            {
                if (!IsAccessTokenExpired(testConfig))
                {
                    return;
                }
                var              oauth2Util = new OAuth2Utility(GetNetUtility());
                string           fullPath   = testConfig.GetFilePath(testConfig.Oauth2.CredentialJsonPath);
                OAuth2Credential credential = InstalledAppCredential.ReadFromFile(fullPath);
                if (credential.ProjectId == null)
                {
                    credential = WebAppCredential.ReadFromFile(fullPath);
                }
                var tokenInfo = oauth2Util.GetTokenInfoAsync(credential, testConfig.Oauth2.RefreshToken).Result;

                testConfig.Oauth2.AccessToken = tokenInfo.AccessToken;
                testConfig.Oauth2.ExpiresIn   = tokenInfo.ExpiresIn;
                testConfig.Oauth2.IssuedTime  = tokenInfo.IssuedTime.ToUniversalTime().ToString("s", System.Globalization.CultureInfo.InvariantCulture) + "Z";
                testConfig.Save();
            }
        }