void exampleFunction() { foreach (string token in TokenRetriever.RetrieveDiscordTokens()) { Console.WriteLine($"Found a token! {token}"); } }
public async Task <string> GetAccessTokenAsync() { var tokenRetriever = new TokenRetriever(new ConfigurationManager()); var scopes = new string[] { "AllSites.FullControl" }; var token = await tokenRetriever.GetTokenByAuthorizationCodeFlowAsync(scopes); return(token.access_token); }
static void Main(string[] args) { // Trust all SSL certs -- needed unless signed SSL certificates are configured. ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); // OAuth configuration var oauthUrl = "https://test.edfi.education.mn.gov/EdFi.Ods.WebApi/"; var clientKey = "j57lM8oL5S7O"; var clientSecret = "h9kgUASBEQhojU8HiSCsNPjz"; // RestSharp dependency, install via NuGet var client = new RestClient("https://test.edfi.education.mn.gov/EdFi.Ods.WebApi/api/v2/2018/"); // TokenRetriever makes the OAuth calls var tokenRetriever = new TokenRetriever(oauthUrl, clientKey, clientSecret); // Plug OAuth into RestSharp's authentication scheme client.Authenticator = new BearerTokenAuthenticator(tokenRetriever); // GET schools var api = new SchoolsApi(client); var response = api.GetSchoolsAll(null, null); // offset, limit var httpReponseCode = response.StatusCode; // returns System.Net.HttpStatusCode.OK Console.WriteLine("Response code is " + httpReponseCode); Console.WriteLine(); // Create identity example setup //var IdentityApi = new identitiesApi(client, 2019); //var Identitiesresponse = IdentityApi.IdentitiesV2_Create(null); // Response data is serialized directly to typed model classes List <School> schools = response.Data; foreach (var school in schools) { Console.WriteLine(school.schoolId + ": " + school.nameOfInstitution); } Console.WriteLine(); Console.WriteLine("Hit ENTER key to continue..."); Console.ReadLine(); }
private IRestClient EstablishApiClient() { // Ignore SSL errors for now ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; var conf = configurationService.GetConfiguration(); var odsapiurlbase = conf.API_SERVER_URL; var odsapioauthurl = Common.Helpers.UrlUtility.GetUntilOrEmpty(odsapiurlbase, "/api/"); var odsapikey = conf.API_SERVER_KEY; var odsapisecret = conf.API_SERVER_SECRET; IRestClient client = null; TokenRetriever tokenRetriever = new TokenRetriever(odsapioauthurl, odsapikey, odsapisecret); client = new RestClient(odsapiurlbase); client.Authenticator = new BearerTokenAuthenticator(tokenRetriever); return(client); }
private static async Task Main() { { string ReadFile(string filename) => File.ReadAllText(filename).Replace("\r\n", "\n"); // Token retrieved from static TokenRetriever class to prevent token theft (TokenRetriever will not be available on GitHub) Steed bot = new Steed(TokenRetriever.RetrieveToken(), ReadFile(@"Commands\Commands.txt"), ReadFile(@"Commands\Admin\AdminCommands.txt")); #if !DEBUG bot.RegisterCommandClass <CommandActions>(); bot.RegisterCommandClass <AdminCommandActions>(); #else bot.RegisterDebugCommandClass <CommandActions>(); bot.RegisterDebugCommandClass <AdminCommandActions>(); #endif // !DEBUG await bot.StartAsync(); } await Task.Delay(-1); }
public void ShouldSuccessfullyRetrieveBearerToken() { var config = new TestOAuthConfiguration { Url = _configuration["OdsApi:OAuthUrl"], Key = _configuration["OdsApi:Key"], Secret = _configuration["OdsApi:Secret"] }; Console.WriteLine(config.Url); Console.WriteLine($"key: {config.Key}"); Console.WriteLine($"secret: {config.Secret}"); var tokenRetriever = new TokenRetriever(config); var tokenHandler = new OAuthTokenHandler(tokenRetriever); var token = tokenHandler.GetBearerToken(); Console.WriteLine($"token: {token}"); Assert.IsTrue(!string.IsNullOrEmpty(token)); }
public void ShouldSuccessfullyRetrieveBearerToken() { var sandboxCredentials = SandboxCredentialsHelper.GetMinimalSandboxCredential(); var config = new TestOAuthConfiguration { Url = Settings.Default.OauthUrl, Key = sandboxCredentials.Key, Secret = sandboxCredentials.Secret }; Console.WriteLine(config.Url); Console.WriteLine($"key: {config.Key}"); Console.WriteLine($"secret: {config.Secret}"); var tokenRetriever = new TokenRetriever(config); var tokenHandler = new OAuthTokenHandler(tokenRetriever); var token = tokenHandler.GetBearerToken(); Console.WriteLine($"token: {token}"); Assert.IsTrue(!string.IsNullOrEmpty(token)); }
public bool ConfigurationIsOk(string apiServerUrl, string apiServerKey, string apiServerSecret) { try { System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderLocal, certificate, chain, sslPolicyErrors) => true; var oAuthUrl = Common.Helpers.UrlUtility.GetUntilOrEmpty(apiServerUrl.Trim(), "/api/"); var client = new RestClient(apiServerUrl); var tokenRetriever = new TokenRetriever(oAuthUrl, apiServerKey, apiServerSecret); client.Authenticator = new BearerTokenAuthenticator(tokenRetriever); var api = new SchoolsApi(client); var apiCall = api.GetSchoolsAll(0, 1); return(apiCall.IsSuccessful); } catch (Exception e) { LogService.Error("Configuration for API failed.", e); return(false); } }
public async Task AddSiteCollection() { // Arrange var configuration = Substitute.For <IConfiguration>(); var log = Substitute.For <ILogger>(); var tokenRetriever = new TokenRetriever(configuration); var tokenCacheService = Substitute.For <ITokenCacheService>(); var tokenManager = new TokenManager(configuration, tokenCacheService, log); var scopes = new string[] { $"api://{configuration["ClientId"]}/access_as_user" }; var siteCollection = new SiteCollection() { Url = $"https://{configuration["SharePointTenantPrefix"]}.sharepoint.com/sites/rztest123", Owner = $"{configuration["Username"]}", Title = "Test123", Template = "STS#0", StorageMaximumLevel = 100, UserCodeMaximumLevel = 300 }; // Act var uri = await tokenManager.GetAuthUriAsync(scopes); var authCode = await tokenRetriever.GetAuthCodeByMsalUriAsync(uri); var authResult = await tokenManager.GetAccessTokenFromCodeAsync(authCode, scopes); //var sharepointManager = new SharePointManager(configuration, tokenManager, authResult.AccessToken); //var result = await sharepointManager.CreateSiteCollectionAsync(siteCollection); //// Assert //Assert.NotNull(result); //Assert.False(result.HasError); }