0

I've run into a problem of not having the slightest idea how to authenticate with my back-end service in order to create a new subscription in a event grip topic using REST API. I've been stuck here for quite a while now, could anyone give m some pointers?

I managed to create the subscription using my own user access token, so everything regarding creation logic works. The only problem is authentication/authorization.

Musius
  • 55
  • 7
  • Do you mean to get access token to use this [REST API](https://learn.microsoft.com/en-us/rest/api/apimanagement/2019-12-01/subscription/createorupdate)? – unknown Oct 13 '20 at 06:22
  • Yep, that's the way I'd prefer, unless there are other, more recommended, ways – Musius Oct 13 '20 at 06:23

1 Answers1

0

Here is the sample about getting the access token with c#:

var clientId = "your-application-id";
var clientSecret = "client-Secret";
var tenantId = "your-tenant-id";
var clientCredential = new ClientCredential(clientId, clientSecret);
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
var accessToken = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result.AccessToken;

Console.WriteLine(accessToken);

Then refer to this sample for calling REST API with HttpClient.

unknown
  • 6,778
  • 1
  • 5
  • 14
  • Thanks, will check it out and let you know how it went. =) – Musius Oct 13 '20 at 06:38
  • Hmm, I get the token, but it still returns 401. Just now saw the error message, continuing to investigate – Musius Oct 13 '20 at 08:33
  • The received access token is not valid: at least one of the claims 'puid' or 'altsecid' or 'oid' should be present. If you are accessing as application please make sure service principal is properly created in the tenant. – Musius Oct 13 '20 at 10:32
  • If I authenticate using Azure CLI, I manage to get the valid token. If I do it in code - I get the error specified above. Puid exist in the token acquired by Azure CLI. – Musius Oct 13 '20 at 10:51
  • Yep, managed to get it working with this snippet, Thanks! Can you link me to the documentation you found it in? – Musius Oct 14 '20 at 11:44
  • Yes! I refer to this [document](https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow) and this [answer](https://stackoverflow.com/a/40504150/13308381). – unknown Oct 15 '20 at 01:02