I am struggling implementing Oracle json DB.
When I run this:
curl -i -H “Authorization: Bearer VFQzfKCN0C1wkqyxTAR3yw” -X GET https://xxxxxx-yyy.adb.eu-stockholm-1.oraclecloudapps.com/ords/peka/soda/latest/
The above curl work as expected without any problems.
When implementing the same function in c# code I get:
DB Error: Unauthorised”
I know the AccessToken works as it is the same I use in the above curl.
Here is the code:
using Unity.Services.CloudCode.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ReadPlayerRecord;
public class MyModule
{
public string url = "xxxxxx-yyy.adb.eu-stockholm-1.oraclecloudapps.com";
public string clientId = "zzzzz";
public string clientSecret = "mmmm";
private string? msg;
public AccessToken? accessToken;
private static readonly HttpClient client = new HttpClient();
[CloudCodeFunction("ReadPlayerRecord")]
public async Task<string> ReadPlayerRecord()
{
msg = ">>ReadPlayerRecord<<n";
await GetAccessTokenAsyncAndReadJson();
return msg;
}
public async Task GetAccessTokenAsyncAndReadJson()
{
msg += ">> GetAccessToken <<n";
var plainTextBytes = Encoding.UTF8.GetBytes(clientId + ":" + clientSecret);
var encoded = Convert.ToBase64String(plainTextBytes);
using (var client = new HttpClient())
{
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials")
});
client.DefaultRequestHeaders.Add("Authorization", "Basic " + encoded);
var response = await client.PostAsync("https://" + url + "/ords/peka/oauth/token", formContent);
if (response.IsSuccessStatusCode)
{
var resultContent = await response.Content.ReadAsStringAsync();
accessToken = JsonConvert.DeserializeObject<AccessToken>(resultContent);
msg += "nAccessToken: " + accessToken?.access_token + "n";
msg += ">> SODA <<n";
Dictionary<string, string> headers = new Dictionary<string, string>();
headers["Authorization"] = "Bearer " + accessToken?.access_token;
msg += "https://" + url + "/ords/peka/soda/latestn"; //fruitn";
var urlResponse = await client.GetAsync("https://" + url + "/ords/peka/soda/latest"); //"/fruit");
if (urlResponse.IsSuccessStatusCode)
{
var urlResultContent = await urlResponse.Content.ReadAsStringAsync();
msg = urlResultContent;
}
else
{
msg += "DB Error: " + urlResponse.StatusCode + "n";
}
}
else
{
msg += "Access Token Error: " + response.StatusCode + "n";
}
}
}
}
[Serializable]
public class AccessToken
{
public string? access_token { get; set; }
public string? token_type { get; set; }
public int? expires_in { get; set; }
}
Here is the output:
RESPONSE: _jsonResult: >>ReadPlayerRecord<<
>> GetAccessToken <<
AccessToken: VFQzfKCN0C1wkqyxTAR3yw
>> SODA <<
https:// xxxxxx-yyy..adb.eu-stockholm-1.oraclecloudapps.com/ords/peka/soda/latest
DB Error: Unauthorized
Would anyone nice be able to help me figure out what is wrong with my c# code as it works using curl?