I am trying to figure out a way to get oauth2.0 bearer token from the rest API.
So from Postman I can do a POST method for "https://helloworld.org:443/xyz/oauth2/token"
And in the body, I choose x-www-form-urlencoded and put grant_type as "password" and put the values for my username and password. This gives me a token which is a bearer token and I use this to get data out of the API endpoints.
I want to do this in SSIS and I cannot use the web service task because this API is a REST API and not SOAP so it does not have any services that returns the token back. I have to do it in a script task. When I run the script task I get this error "Error occurred while fetching the bearer token: Not Found"
Below is my script task code:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace ST_9e6c1ec9747f4bca8db831645eddf61d
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// Variables to store the credentials and API endpoint
string username = "sadao002";
string password = "dsf83837%%";
string apiEndpoint = "https://helloworld.org:443/xyz/oauth2/token";
// Create an HttpClient instance
using (HttpClient client = new HttpClient())
{
// Set the base address of the API
client.BaseAddress = new Uri(apiEndpoint);
// Define the content (username, password, and grant_type) to be sent as form data
var formData = new System.Collections.Generic.Dictionary<string, string>
{
{ "username", username },
{ "password", password },
{ "grant_type", "password" }
};
var content = new FormUrlEncodedContent(formData);
// Set the content type header
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
// Send the request and receive the response
var response = client.PostAsync("/token", content).Result;
if (response.IsSuccessStatusCode)
{
// Read the response content as string
string responseBody = response.Content.ReadAsStringAsync().Result;
// Assuming the response contains a JSON object with an "access_token" field for the bearer token
// You can parse the JSON manually, or using System.Text.Json.JsonSerializer if you're using .NET Core 3.0 or later.
// For simplicity, we'll just look for "access_token":"value" pattern.
int tokenIndex = responseBody.IndexOf("\"access_token\":\"");
if (tokenIndex >= 0)
{
tokenIndex += "\"access_token\":\"".Length;
int tokenEndIndex = responseBody.IndexOf("\"", tokenIndex);
string bearerToken = responseBody.Substring(tokenIndex, tokenEndIndex - tokenIndex);
// Now you have the bearer token, you can use it for your subsequent requests.
// For example, you can store it in a package variable or use it in the same script task.
MessageBox.Show("Bearer Token: " + bearerToken, "Token Fetched Successfully");
}
else
{
MessageBox.Show("Unable to fetch bearer token. Response does not contain a valid token.", "Error");
}
}
else
{
MessageBox.Show("Error occurred while fetching the bearer token: " + response.ReasonPhrase, "Error");
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
public enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
}
}
}
My questions are:
Do i have to include the port number in the apiEndpoint?
In this line, is it adding "/token" to the call?
var response = client.PostAsync("/token", content).Result
Thanks for any help.
I tried the above code and a lot of googling and research.