Announcement

Collapse
No announcement yet.

Help with the API please

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with the API please

    Hi, I am trying to use the API as documented here: https://www.espocrm.com/documentatio...pi-client-php/

    I have created a new instance on the cloud server for testing, and have copied and pasted the sample code at the end of the above article.

    The code is throwing the exception on line 131 of the sample. For clarity here are the lines of code either side of that exception:


    PHP Code:
            if ($responseCode == 200 && !empty($parsedResponse['body'])) {
                
    curl_close($ch);
                return 
    json_decode($parsedResponse['body'], true);
            }
            
    $header $this->normalizeHeader($parsedResponse['header']);
            
    $errorMessage = !empty($header['X-Status-Reason']) ? $header['X-Status-Reason'] : 'EspoClient: Unknown Error';
             
    curl_close($ch);
            throw new 
    \Exception($errorMessage$responseCode);
        } 

    Using a bit of debug I have worked out that the error responseCode is 404 and the errorMessage is 'EspoClient: Unknown Error'

    The URL being called is https://s4-applications.espocloud.eu//api/v1/Lead which is valid.

    Help and guidance appreciated.

    Thanks.
    Last edited by MatLudlam; 08-09-2019, 06:25 PM.

  • #2
    Hello,

    could you provide a little more information about how you call the API?
    Did you create an API user and are you passing either login credentials or the auth-token?

    You can find more information here: https://www.espocrm.com/documentation/development/api/

    Greetings

    Comment


    • #3
      Thanks for getting back to me. Here is the code that I am using for the call. Note that the environment being called is the default one, with default data in the cloud environment.

      Whilst I would not normally publish the API key in a post, there is nothing in that environment. Once I have it working, I will move it to our production environment.


      PHP Code:
              <?php
              
      include_once 'EspoApiClient.php';
              print 
      'about to call';
              
      // put your code here
              
      $client = new EspoApiClient('https://s4-applications.espocloud.eu/');
              
      $client->setApiKey('3418f1493dc7fce74141c479cd67bcfc');

              
      $response $client->request('POST''Lead', [
                  
      'firstName' => 'Test',
                  
      'lastName' => 'Hello'
              
      ]);
              print 
      'response was';
              print 
      $response;
              
      ?>


      Here are 2 screen shots of the API user's configuration:
      Click image for larger version

Name:	API User Conf1.PNG
Views:	473
Size:	26.1 KB
ID:	50820



      Here is the security permission section.
      Click image for larger version

Name:	API User Conf2.PNG
Views:	362
Size:	3.4 KB
ID:	50821

      Comment


      • MatLudlam
        MatLudlam commented
        Editing a comment
        I have checked messing around with the API key, if it is incorrect I get an error 401. If it is correct, I get the 404.

        I can also confirm that you can create a Lead with just firstName and lastName. No other data is required.
        Last edited by MatLudlam; 08-13-2019, 07:41 AM.

    • #4
      I have done a bit more digging, and the output of the command:

      PHP Code:
      $this->lastResponse curl_exec($ch); 
      within the request method is:

      Code:
      HTTP/1.1 404 Not Found
      Server: nginx
      Date: Tue, 13 Aug 2019 07:51:28 GMT
      Content-Type: text/html;charset=UTF-8
      Transfer-Encoding: chunked
      Connection: keep-alive
      X-Status-Reason: Controller 'Api' is not found
      I have also dumped the URL being called, and that is:
      Code:
      https://s4-applications.espocloud.eu//api/v1/Lead

      Comment


      • #5
        Further digging around, it seems that within the URL there are two "/" characters between the domain and the text "api". I fixed this and now get the following error:

        Code:
        HTTP/1.1 403 Forbidden
        Server: nginx
        Date: Tue, 13 Aug 2019 07:57:08 GMT
        Content-Type: text/html;charset=UTF-8
        Transfer-Encoding: chunked
        Connection: keep-alive
        X-Status-Reason: Assignment permission failure.
        I am guessing this is a user permissions thing. The API user already has the permission to create Leads (see above), so what else should I do?

        Thanks.

        Comment


        • #6
          Ok, I have done further testing and I have done the following:
          • Created a new role with the permissions that I need
          • Assigned those permissions to the API user that I am using

          My Code now looks like the following, feel free to try it at your end, the API key in there is valid. This is 100% test data.
          PHP Code:
                  include_once 'EspoApiClient.php';

                  
          // put your code here
                  
          $client = new EspoApiClient('https://s4-applications.espocloud.eu');
                  
          $client->setApiKey('3418f1493dc7fce74141c479cd67bcfc');

                  
          $response $client->request('GET''Account?offset=0&maxSize=20'); // Works
                  
          $response $client->request('GET''Account/53203b942850b');       // Works
                  
          $response $client->request('GET''Lead/5d5af4b56871fea39');      // Works

                  // Throws exception caused by 403 error
                  
          $response $client->request('POST''Lead', [
                      
          'firstName' => 'Test',
                      
          'lastName' => 'Hello',
                      
          'name' => 'Test Hello',
                      
          'status' => 'New',
                  ]); 

          The response from the server is:
          HTTP/1.1 403 Forbidden
          Server: nginx
          Date: Mon, 19 Aug 2019 19:18:41 GMT
          Content-Type: text/html;charset=UTF-8
          Transfer-Encoding: chunked
          Connection: keep-alive
          X-Status-Reason: Assignment permission failure.

          Comment


          • #7
            Further update ..

            I decided to test against an in-house install (5.6.11) and that worked just fine.

            So I need to work out why the cloud instance is not behaving the same as the personal instance.

            Comment

            Working...
            X