Legacy Dev Forum Posts

 View Only

Sign Up

Place and outbound Call via API and Sip SoftPhone

  • 1.  Place and outbound Call via API and Sip SoftPhone

    Posted 06-05-2025 18:05

    masanchezmemo | 2018-08-29 14:20:59 UTC | #1

    Hello there,

    We start developing this solution using the following code:

    string CLIENTID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; string CLIENTSECRET ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; var accessTokenInfo = Configuration.Default.ApiClient.PostToken(CLIENTID, CLIENTSECRET); string accessToken_ = accessTokenInfo.AccessToken;

    Configuration.Default.AccessToken = accessToken_;

    var apiConversations = new ConversationsApi(); CreateCallRequest createCallRequest = new CreateCallRequest(); createCallRequest.PhoneNumber = "+593981224064"; apiConversations.PostConversationsCalls(createCallRequest);

    When we were performing the test, we got an exception just in the final code line, with the message {“status”:400,“code”:“not.a.user”,“message”:“This request requires a user context. Client credentials cannot be used for requests to this resource.”}.

    We look in the forum that this message is related with the type of authentication that we are using (Client Credentials), we decide to create an OAuth with Implicit Authentication, then we create the properly code for this type of authentication following the implicit_winform example of purecloud, we test the connection successfully and then we include the following code for our purpose:

    HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("https://api.mypurecloud.com/api/v2/conversations/calls"); request2.Method = "POST"; request2.ContentType = "application/json"; request2.Headers.Add("Authorization", "Bearer " + bearerToken);

    using (var streamWriter = new StreamWriter(request2.GetRequestStream())) { string json = "{\"phoneNumber\":\"+593981224064\"}";

    streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); }

    WebResponse webResponse = request2.GetResponse(); using (Stream webStream = webResponse.GetResponseStream()) { if (webStream != null) { using (StreamReader responseReader = new StreamReader(webStream)) { string response = responseReader.ReadToEnd(); Console.Out.WriteLine(response); txtInfo.Text = response; } } }

    Now when we are running the test we go an exception in the line request2.GetResponse(); and the message said “status”: 500, “code”: “internal.server.error”.

    We know that the authentication process is good, because if we call the https://api.mypurecloud.com/api/v2/users/me we got the correct response.

    Another important thing is that we know the softphone is properly installed because if we use the developer tools (both in Api Explorer and Code Editor) we can perform the call just with the following code: { phoneNumber: "+593981224064" }

    Any clue?

    regards,

    Mario


    tim.smith | 2018-08-29 20:08:02 UTC | #2

    Can you provide the correlation ID from the 5xx response?

    Any reason you're not using the SDK to place the call in the second example?

    As a side note, I'm not sure how you've implemented the login to your app, but there is a packaged control that will handle the process for you: https://github.com/MyPureCloud/purecloud_api_dotnet_oauth_control


    masanchezmemo | 2018-08-31 14:03:55 UTC | #3

    thanks Tim, My developer made the code work based on the SDK's samples.

    Tks again.

    Cheers.


    masanchezmemo | 2018-09-10 16:31:38 UTC | #4

    Tim,

    Now under normal circumstance can place and outbound call and off course disconnect it. The question here is how to handle scenarios where I have no lines to place the call and I get a response tone of fast busy, how should I disconnect that kind ot call tries since I have no conversation ID.

    Regards,

    Mario


    tim.smith | 2018-09-10 21:33:30 UTC | #5

    You should be getting a conversation ID in the response to POST /api/v2/conversations/calls since the conversation ID is generated before the call is even attempted. Are you not seeing the conversation ID in the response?


    masanchezmemo | 2018-09-11 13:03:53 UTC | #6

    Tim, here it is a more detailed explanation:

    In our code, we are using Asp language, first, we try to use the purecloud API calling the method PostConversationsCall(), at this time we were using the Client Credentials as authentication method, but when we execute the code we got an exception with the message ”not.a.user”, then we find out in the forum that if we have this error message is because we are using an inappropriate type of authentication.

    We implement an Implicit authentication for our web Application, but at this time we just find a Windows Forms examples in C#, we have troubles when we try to replicate this code in a Web environment, then we find an example using Javascript for this authentication method and we implement successfully in our example code, at this time we were able to authenticate and perform a call successfully, but the problem here is when we invoke the authentication method from an asp.bottom that is located inside an HTML <form>, this particularity doesn't allow to make the web redirection to obtain the access token via URL, the only way to implement this process it was outside the HTML <form>.

    In our web application, we have to call this process from a Button that is located inside the HTML <form>, we can not change this structure because we are performing other validations and query process at this time, and we decide to implement a popup to perform the authentication in the onload event and perform the call automatically, this solution is working but it's not an optimized solution, if you have a better way to implement it will be helpful for us.

    In the other hand, we are now trying to call the API to get the current active conversations for this user, for this code we are using the client credentials authentication when we execute this code we have no errors but we didn't get any active call, but if we execute this functionality from de developer tool we can see that there is an active call, we were thinking about this and maybe here we have to perform an implicit authentication because we need a user, we didn't test this but please let us know if this is correct or not because if we have to use this type of authentication we will have to implement another popup for this purpose and we want to avoid this at this point.

    Regards,

    Mario


    tim.smith | 2018-09-11 13:10:03 UTC | #7

    masanchezmemo, post:6, topic:3459
    We implement an Implicit authentication for our web Application, but at this time we just find a Windows Forms examples in C#

    There's an ASP.NET Auth Code tutorial.

    masanchezmemo, post:6, topic:3459
    In the other hand, we are now trying to call the API to get the current active conversations for this user, for this code we are using the client credentials authentication when we execute this code we have no errors but we didn't get any active call, but if we execute this functionality from de developer tool we can see that there is an active call, we were thinking about this and maybe here we have to perform an implicit authentication because we need a user, we didn't test this but please let us know if this is correct or not because if we have to use this type of authentication we will have to implement another popup for this purpose and we want to avoid this at this point.

    What APIs are you using? The dev tools logs you in as a user, so whatever you're doing there will be in the context of the user. If your app has humans using it, you shouldn't be using client credentials at all. Use the user's auth token to make API requests on behalf of that user.


    masanchezmemo | 2018-09-14 01:17:31 UTC | #8

    Solved Tim, you are the one!!!!:slight_smile: Thanks a lot!!

    Regards,

    Mario


    system | 2018-10-15 01:22:31 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: 3459