Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions dotnet-cognito-auth/donet-api-gateway-auth/ApiHelper.cs
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";
Copy link
Contributor

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

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;
}
}
}
15 changes: 15 additions & 0 deletions dotnet-cognito-auth/donet-api-gateway-auth/Bearer.cs
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; }
}
}
27 changes: 27 additions & 0 deletions dotnet-cognito-auth/donet-api-gateway-auth/Program.cs
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}");
}
}

}



}
7 changes: 7 additions & 0 deletions dotnet-cognito-auth/donet-api-gateway-auth/README.md
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();
```
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>