-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.NET api call example #5
Open
faabebe
wants to merge
1
commit into
africa-covid-19-response-toolkit:master
Choose a base branch
from
faabebe:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace donet_api_gateway_auth | ||
{ | ||
public static class ApiHelper | ||
{ | ||
private static string GenerateOauthToken() | ||
{ | ||
//TODO: pull this from env vars | ||
string consumerKey = "5mulvgjjlbbm4cb8p7s1d6li8i"; | ||
string consumerSecret = "1pv83kd7vu9dps5alriaj25c5hfdiqvl3d9ld41dupjlne5goj1q"; | ||
string accessToken; | ||
|
||
byte[] byte1 = Encoding.ASCII.GetBytes("grant_type=client_credentials"); | ||
|
||
HttpWebRequest bearerReq = WebRequest.Create("https://et-covid20.auth.us-east-2.amazoncognito.com/oauth2/token") as HttpWebRequest; | ||
bearerReq.Accept = "application/json"; | ||
bearerReq.Method = "POST"; | ||
bearerReq.ContentType = "application/x-www-form-urlencoded"; | ||
bearerReq.ContentLength = byte1.Length; | ||
bearerReq.KeepAlive = false; | ||
bearerReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(consumerKey + ":" + consumerSecret))); | ||
Stream newStream = bearerReq.GetRequestStream(); | ||
newStream.Write(byte1, 0, byte1.Length); | ||
|
||
WebResponse bearerResp = bearerReq.GetResponse(); | ||
|
||
using (var reader = new StreamReader(bearerResp.GetResponseStream(), Encoding.UTF8)) | ||
{ | ||
var response = reader.ReadToEnd(); | ||
Bearer bearer = JsonConvert.DeserializeObject<Bearer>(response); | ||
accessToken = bearer.access_token; | ||
} | ||
|
||
return accessToken; | ||
} | ||
|
||
public static HttpWebRequest GetWebRequest(string uri) | ||
{ | ||
string accessToken = ApiHelper.GenerateOauthToken(); | ||
HttpWebRequest apiRequest = WebRequest.Create(uri) as HttpWebRequest; | ||
apiRequest.ContentType = "application/json"; | ||
apiRequest.KeepAlive = false; | ||
apiRequest.Headers.Add("Authorization", "Bearer " + accessToken); | ||
return apiRequest; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace donet_api_gateway_auth | ||
{ | ||
public class Bearer | ||
{ | ||
public string scope { get; set; } | ||
public string token_type { get; set; } | ||
public string expires_in { get; set; } | ||
public string refresh_token { get; set; } | ||
public string access_token { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace donet_api_gateway_auth | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
HttpWebRequest apiRequest = ApiHelper.GetWebRequest("https://api.ethiopia-covid19.com/gateway/communities"); | ||
apiRequest.Method = "GET"; | ||
WebResponse apiResponse = apiRequest.GetResponse(); | ||
using (var reader = new StreamReader(apiResponse.GetResponseStream(), Encoding.UTF8)) | ||
{ | ||
var response = reader.ReadToEnd(); | ||
Console.WriteLine($"Api Response: {response}"); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Example | ||
|
||
``` | ||
HttpWebRequest apiRequest = AuthHelper.GetWebRequest("https://api.ethiopia-covid19.com/gateway/communities"); | ||
apiRequest.Method = "GET"; | ||
WebResponse apiResponse = apiRequest.GetResponse(); | ||
``` |
13 changes: 13 additions & 0 deletions
13
dotnet-cognito-auth/donet-api-gateway-auth/donet-api-gateway-auth.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<RootNamespace>donet_api_gateway_auth</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Client id and secret should not be committed, please add them to environment variables