Legacy Dev Forum Posts

 View Only

Sign Up

How to get user id of a client credential user

  • 1.  How to get user id of a client credential user

    Posted 06-05-2025 18:06

    rita.hou | 2022-06-10 18:22:33 UTC | #1

    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?


    Eos_Rios | 2022-06-11 14:53:54 UTC | #2

    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.uswest2;

    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 ) );`


    rita.hou | 2022-06-10 20:39:47 UTC | #3

    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}, { "clientsecret", clientsecret }, { "granttype", "clientcredentials" } }); var response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); var toeknRequest = Newtonsoft.Json.Linq.JObject.Parse(await response.Content.ReadAsStringAsync()); var token = toeknRequest.Value<string>("accesstoken");

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


    tim.smith | 2022-06-10 20:56:56 UTC | #4

    @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.


    rita.hou | 2022-06-10 21:08:04 UTC | #5

    @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":"invalidclient","description":"client not found","errordescription":"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);


    tim.smith | 2022-06-14 20:40:31 UTC | #6

    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


    Eos_Rios | 2022-06-11 14:55:29 UTC | #7

    @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.


    rita.hou | 2022-06-13 14:41:13 UTC | #8

    Eos_Rios, post:2, topic:15092
    PureCloudRegionHosts region = PureCloudRegionHosts.us_west_2;
    
      Configuration.Default.ApiClient.setBasePath( region );

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


    system | 2022-07-14 14:41:18 UTC | #9

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


    This post was migrated from the old Developer Forum.

    ref: 15092