How to get user id of a client credential user

I'm working on a .NET app code to get notifications for historical adherence data, the authentication I'm using is client credential, the auth part works fine, I can get the bearer token without issues. But when I send /v2/users/me request to get the user id in order to subscribe to the notifications, I get 400 bad request with error "This request requires a user context. Client credentials cannot be used for requests to this resource". How can I get user id to subscribe to the channel if client credential user is not a user?

Your client id is your user id for notification purposes.
You don't need to ask who your user is, you authed as it already and should know.

  PureCloudRegionHosts region = PureCloudRegionHosts.us_west_2;

  Configuration.Default.ApiClient.setBasePath( region );
AuthTokenInfo accessTokenInfo = Configuration.Default.ApiClient.PostToken( Client.ClientId , Client.ClientSecret );

NotificationHandler.AddSubscription( $"v2.users.{Client.ClientId}.workforcemanagement.historicaladherencequery" , typeof( WfmHistoricalAdherenceResponse ) );`
1 Like

Thank you Eos_Rios!

I'm able to get access token with my client id and client secret, but then when I make the call to notification handler it throws an error of PureCloudPlatform.Client.V2.Client.ApiException: 'Error calling PostNotificationsChannels: {"message":"No authentication bearer token specified in authorization header.","code":"authentication.required","status":401,"contextId":"8e9c75f3-a4f3-4d0d-b890-bdc7f004cc40","details":[],"errors":[]}'

Here's my code, do I need to add the token to the notification handler request?

var client = new HttpClient { };
var request = new HttpRequestMessage(HttpMethod.Post, "https://login.{env}.pure.cloud/oauth/token");
request.Content = new FormUrlEncodedContent(new Dictionary<string, string> {
{ "client_id", _clientid},
{ "client_secret", _clientsecret },
{ "grant_type", "client_credentials" }
});
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
var toeknRequest = Newtonsoft.Json.Linq.JObject.Parse(await response.Content.ReadAsStringAsync());
var token = toeknRequest.Value("access_token");

        var NotificationHandler = new NotificationHandler();
        NotificationHandler.AddSubscription($"v2.users.{_clientid}.workforcemanagement.historicaladherencequery", typeof(WfmHistoricalAdherenceResponse));

@rita.hou The SDK expects you to use one of the supported methods for authorization. See this section of the SDK readme for more info: https://developer.genesys.cloud/devapps/sdk/dotnet#authenticating.

@tim.smith - I tried to use the code that in the doc and got following error,
PureCloudPlatform.Client.V2.Client.ApiException: 'Error calling PostToken: {"error":"invalid_client","description":"client not found","error_description":"client not found"}

but I'm able to get the access token from my previous code. I'm really confused, do I miss anything?

Here's the code that I used,

        var _clientid = "xxxxx";
        var _clientsecret = "yyyyyyyyy";

        var accessTokenInfo = Configuration.Default.ApiClient.PostToken(_clientid, _clientsecret);
        Console.WriteLine("Access token=" + accessTokenInfo.AccessToken);

If you're not using the us-east region (mypurecloud.com), which I gather you're not based on your sample code's URL, you need to set the environment on the SDK first: https://developer.genesys.cloud/devapps/sdk/dotnet#setting-the-environment

2 Likes

@rita.hou I updated my example to include the region to show what Tim is describing in context.

Using the language specific SDK makes things much easier.

2 Likes

The suggestions work perfect! Thank you @Eos_Rios and @tim.smith

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.