Details:
- What’s your URL? https://grantwinney.com
- What version of Ghost are you using? Ghost-CLI version: 1.9.9 / Ghost version: 2.18.1
- What configuration? self-hosted (?)
- What browser? Brave - Version 0.61.51 Chromium: 73.0.3683.75 (Official Build) (64-bit)
- What errors or information do you see in the console? error below
- What steps could someone else take to reproduce the issue you’re having? my steps are below
I’m trying to experiment with the Admin API, and I think I’m authenticating correctly with an Admin API Key id/secret, but I’m unable to actually authenticate.
missingAdminUserOrIntegration: “Unable to determine the authenticated user or integration. Check that cookies are being passed through if using session authentication.”
Looking at the code, I see this comment on the code:
We don’t support admin api keys yet, but we can already use this authorization helper, because we have not connected authenticating with admin api keys yet.
req.api_key
will be always null.
Is the comment outdated, or (the way I interpret it) can we only authenticate with the Admin API via username/password right now, and not an Admin API Key?
If the comment’s outdated, then I guess I’m just doing something wrong.
This probably won’t mean a ton to anyone, but here’s how I’m trying to authenticate in C#, using the Jwt.Net library.
var adminKeyParts = adminApiKey.Split(':');
var unixEpochInSeconds = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds();
var token = new JwtBuilder().WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(StringToByteArray(adminKeyParts[1]))
.AddHeader(HeaderName.KeyId, adminKeyParts[0])
.AddClaim("exp", unixEpochInSeconds + 300)
.AddClaim("iat", unixEpochInSeconds)
.AddClaim("aud", "/v2/admin/")
.Build();
var request = new RestRequest($"posts/{id}", Method.GET);
request.AddHeader("Authorization", $"Ghost {token}");
var response = Client.Execute<PostResponse>(request);
return response.Data;
...
...
public static byte[] StringToByteArray(string hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}