Menu
SuiteCRM Pull Request Party hosted by SalesAgility
Welcome, Guest
Username: Password: Remember me

TOPIC: API Request

API Request 1 year 1 month ago #63453

  • brendanb
  • brendanb's Avatar
  • Offline
  • Junior Member
  • Posts: 28
  • Thank you received: 2
  • Karma: 2
Yeah, that can always be an issue when running a vm from a different timezone.
The administrator has disabled public write access.

API Request 1 year 1 month ago #63484

  • brendanb
  • brendanb's Avatar
  • Offline
  • Junior Member
  • Posts: 28
  • Thank you received: 2
  • Karma: 2
just an fyi,

I found some issues in custom fields. These have been fixed here.

github.com/salesagility/SuiteCRM/issues/5618

Regards
The administrator has disabled public write access.

API Request 11 months 4 weeks ago #66320

  • p_hirlikar
  • p_hirlikar's Avatar
  • Offline
  • New Member
  • Posts: 2
  • Karma: 0
I was struggling with the same issue. In postman, I removed the header settings to default, and it worked for me
The administrator has disabled public write access.

API Request 11 months 4 weeks ago #66321

  • p_hirlikar
  • p_hirlikar's Avatar
  • Offline
  • New Member
  • Posts: 2
  • Karma: 0
Once you get the token, you enable the headers listed in the documentation.

Hope this helps.
The administrator has disabled public write access.

API Request 11 months 4 weeks ago #66327

  • judgerod
  • judgerod's Avatar
  • Offline
  • Junior Member
  • Posts: 21
  • Karma: 0
I notice in your code below that you don't have $ in front of the url variable in curl_setopt($ch, CURLOPT_URL, url). Try changing it and see if it works.

$url = 'https://path-to-instance/api/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');


Also there is a tick mark at the end of this line: 'Accept: application/vnd.api+json',`
That's a typo in the example.
Last Edit: 11 months 4 weeks ago by judgerod.
The administrator has disabled public write access.

API Request 10 months 2 weeks ago #68925

  • ideiamais
  • ideiamais's Avatar
  • Offline
  • New Member
  • Agregando Valor
  • Posts: 8
  • Thank you received: 2
  • Karma: 0
Does anyone know where to find the api / oauth directory of this example www.mysite.com/api/oauth/access_token?
The administrator has disabled public write access.

API Request 10 months 1 week ago #69146

  • fanton.ff
  • fanton.ff's Avatar
  • Offline
  • Senior Member
  • Posts: 68
  • Thank you received: 6
  • Karma: 5
There's no "api/oauth" directory, it's a "virtual directory" managed by lib/API/public/index.php.
Maybe you need to regenerate .htaccess files from the Admin menu
The administrator has disabled public write access.
The following user(s) said Thank You: ideiamais

API Request 9 months 1 week ago #70369

  • dynorodney
  • dynorodney's Avatar
  • Offline
  • New Member
  • Posts: 11
  • Karma: 0
Hi @hopley

Did you solve this problem at all ? Im having the same issue as you although the message changed slightly in 7.10.7 from "Client authentication failed" to "The resource owner or authorization server denied the request"

It seems to be to do with the Authorization header not being passed in but the sample code doesnt pass an Authorization header in as part of the access token request - its only when you make further requests that the access token is passed into the call as an Authorization header

Ive tried setting the timezone in my php code but i was also just using a REST client like postman and still getting the same issues and obviously you cant set the timezone there anyway. Ive rebuilt the htaccess too but with no effect

Thanks in advance, for any pointers if you did solve it
The administrator has disabled public write access.

API Request 9 months 1 week ago #70370

  • fanton.ff
  • fanton.ff's Avatar
  • Offline
  • Senior Member
  • Posts: 68
  • Thank you received: 6
  • Karma: 5
Can you show us your code and what suitecrm.log says?
Last Edit: 9 months 1 week ago by fanton.ff.
The administrator has disabled public write access.

API Request 9 months 1 week ago #70373

  • dynorodney
  • dynorodney's Avatar
  • Offline
  • New Member
  • Posts: 11
  • Karma: 0
Hi

Thanks for the response

I've done the process to create the oauth2 public and private keys as described docs.suitecrm.com/developer/api/version-8/configure-suitecrm/ and created the client credentials id/secret

Have then copied the example code from docs.suitecrm.com/developer/api/version-...gure-authentication/, fixed the 2 errors from the code and added my client credentials details (obfuscated below) . I added the UTC timezone as one of the posts I read implied that might help the problem but it had no effect.
<?php
date_default_timezone_set('UTC');

$ch = curl_init();
$header = array(
    'Content-type: application/vnd.api+json',
    'Accept: application/vnd.api+json' );
$postStr = json_encode(array(
    'grant_type' => 'client_credentials',
    'client_id' => '5059c3cf-2687-da1c-d66f-5b6d5a6fcd42',
    'client_secret' => 'TestSecretObfuscated',
    'scope' => 'standard:create standard:read standard:update standard:delete standard:delete standard:relationship:create standard:relationship:read standard:relationship:update standard:relationship:delete'
));
$url = 'https://example.com/api/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$output = curl_exec($ch);

print_r($output);

When I run it, suitecrm.log says
Fri Aug 10 09:28:42 2018 [20950][-none-][FATAL] [ERROR] [ResourceServer]  Code: 9 Message: The resource owner or authorization server denied the request. ErrorType: access_denied Hint: Missing "Authorization" header

and the actual output that Im printing to screen is
{"error":"access_denied","message":"The resource owner or authorization server denied the request.","hint":"Missing \"Authorization\" header"}

My .htaccess is
# BEGIN SUGARCRM RESTRICTIONS
RedirectMatch 403 .*\.log$
RedirectMatch 403 /+not_imported_.*\.txt
RedirectMatch 403 /+(soap|cache|xtemplate|data|examples|include|log4php|metadata|modules)/+.*\.(php|tpl)
RedirectMatch 403 /+emailmandelivery\.php
RedirectMatch 403 /+upload
RedirectMatch 403 /+cache/+diagnostic
RedirectMatch 403 /+files\.md5$
<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule ^cache/jsLanguage/(.._..).js$ index.php?entryPoint=jslang&modulename=app_strings&lang=$1 [L,QSA]
    RewriteRule ^cache/jsLanguage/(\w*)/(.._..).js$ index.php?entryPoint=jslang&modulename=$1&lang=$2 [L,QSA]
    RewriteRule ^cache/jsLanguage/(.._..).js$ index.php?entryPoint=jslang&module=app_strings&lang=$1 [L,QSA]
    RewriteRule ^cache/jsLanguage/(\w*)/(.._..).js$ index.php?entryPoint=jslang&module=$1&lang=$2 [L,QSA]
    RewriteRule ^api/(.*?)$ lib/API/public/index.php/$1 [L]
    RewriteRule ^api/(.*)$ - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
# END SUGARCRM RESTRICTIONS
# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
   php_flag display_errors Off
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 128M
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php72"
   php_value upload_max_filesize 8M
   php_flag zlib.output_compression On
</IfModule>
# END cPanel-generated php ini directives, do not edit

<FilesMatch "\.(jpg|png|gif|js|css|ico)$">
        <IfModule mod_headers.c>
                Header set ETag ""
                Header set Cache-Control "max-age=2592000"
                Header set Expires "01 Jan 2112 00:00:00 GMT"
        </IfModule>
</FilesMatch>
<IfModule mod_expires.c>
        ExpiresByType text/css "access plus 1 month"
        ExpiresByType text/javascript "access plus 1 month"
        ExpiresByType application/x-javascript "access plus 1 month"
        ExpiresByType image/gif "access plus 1 month"
        ExpiresByType image/jpg "access plus 1 month"
        ExpiresByType image/png "access plus 1 month"
</IfModule>

I know when you make further calls you pass the JWT that should be received from this call, in as an authorization header but in the initial call you don't pass through an Authorization header, so I can understand it complaining that it doesnt have an authorization header, so I must be doing something wrong!

Its PHP 7.2, Apache 2.4 on Linux

Thanks

Tony
The administrator has disabled public write access.

API Request 9 months 1 week ago #70378

  • fanton.ff
  • fanton.ff's Avatar
  • Offline
  • Senior Member
  • Posts: 68
  • Thank you received: 6
  • Karma: 5
I think that somehow SuiteCRM is receiving the wrong API path... Can you patch lib/API/OAuth2/Middleware/ResourceServer.php like this? Modify the __invoke function and add as first instruction a
$GLOBALS["log"]->fatal($request->getUri()->getPath());

and check suitecrm.log
The administrator has disabled public write access.

API Request 9 months 1 week ago #70379

  • ideiamais
  • ideiamais's Avatar
  • Offline
  • New Member
  • Agregando Valor
  • Posts: 8
  • Thank you received: 2
  • Karma: 0
I tried following the documentation available on the suitecrm website, example of several blog articles, forums response and did not succeed. So I decided to do it for api v4.1. It works perfectly. There is probably a lot to be developed and corrected in this new api, and for the little knowledge I have I do not recommend using it because it is simple and still does not work. If for a simple connection is no way to work, imagine a data integration. Below I've put together a very simple example of easy understanding with only the need to customize for your application.
// quando criar a classe colocar no método construct
$url = "https://your-crm-url/service/v4_1/rest.php";
$username = "your user";
$password = "your pass";

//function to make cURL request
function call($method, $parameters, $url)
{
	ob_start();
	$curl_request = curl_init();

	curl_setopt($curl_request, CURLOPT_URL, $url);
	curl_setopt($curl_request, CURLOPT_POST, 1);
	curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
	curl_setopt($curl_request, CURLOPT_HEADER, 1);
	curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

	$jsonEncodedData = json_encode($parameters);

	$post = array(
		 "method" => $method,
		 "input_type" => "JSON",
		 "response_type" => "JSON",
		 "rest_data" => $jsonEncodedData
	);

	curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
	$result = curl_exec($curl_request);
	curl_close($curl_request);

	$result = explode("\r\n\r\n", $result, 2);
	$response = json_decode($result[1]);
	ob_end_flush();

	return $response;
}


//login -------------------------------------------- 
$login_parameters = array(
	 "user_auth" => array(
		  "user_name" => $username,
		  "password" => md5($password),
		  "version" => "1"
	 ),
	 "application_name" => "RestTest",
	 "name_value_list" => array(),
);

$login_result = call("login", $login_parameters, $url);

//get session id
$session_id = $login_result->id;
   
//create contacts ------------------------------------ 
$set_entries_parameters = array(
	 //session id
	 "session" => $session_id,

	 //The name of the module from which to retrieve records.
	 "module_name" => "Accounts",

	 //Record attributes
	 "name_value_list" => array(
		 array(
			//to update a record, you will nee to pass in a record id as commented below
			//array("name" => "id", "value" => "912e58c0-73e9-9cb6-c84e-4ff34d62620e"),
			array("name" => "name", "value" => "name account"),
			array("name" => "website", "value" => "website account"),
		 ),
		 /*
		 array(
			//to update a record, you will nee to pass in a record id as commented below
			//array("name" => "id", "value" => "99d6ddfd-7d52-d45b-eba8-4ff34d684964"),
			array("name" => "first_name", "value" => "Jane"),
			array("name" => "last_name", "value" => "Doe"),
		 ),
		 */
	 ),
);

$set_entries_result = call("set_entries", $set_entries_parameters, $url);

    echo "<pre>";
    print_r($set_entries_result);
    echo "</pre>";

If you still have questions, please email me at: This email address is being protected from spambots. You need JavaScript enabled to view it.
The administrator has disabled public write access.
The following user(s) said Thank You: dynorodney

API Request 9 months 1 week ago #70380

  • dynorodney
  • dynorodney's Avatar
  • Offline
  • New Member
  • Posts: 11
  • Karma: 0
Hi

Thanks for the quick reply and suggestion.

Fri Aug 10 15:13:48 2018 [7247][-none-][FATAL] /
Fri Aug 10 15:13:48 2018 [7247][-none-][FATAL] [ERROR] [ResourceServer] Code: 9 Message: The resource owner or authorization server denied the request. ErrorType: access_denied Hint: Missing "Authorization" header


It wrote / as a fatal error so it thinks the path is / when it obviously isnt

Because this is running from a subdomain of our main website its set up in cpanel as a subdomain (the server is owned by us, its not shared hosting so we can allocate memory etc as needed) but we use cpanel to manage it. I wondered if cpanel somehow reports the paths differently for a subdomain than if it was a "main site"

I then changed the line to write out the $_SERVER["DOCUMENT_ROOT"] instead and got

Fri Aug 10 15:26:24 2018 [16479][-none-][FATAL] /home/myuser/crm.mydomain.co.uk/SuiteCRM

so that is definitely pointing to the right place

and then got the PHP_SELF

Fri Aug 10 15:28:50 2018 [18690][-none-][FATAL] /lib/API/public/index.php

Is that any more help?

Thanks again

Tony
The administrator has disabled public write access.

API Request 9 months 1 week ago #70381

  • dynorodney
  • dynorodney's Avatar
  • Offline
  • New Member
  • Posts: 11
  • Karma: 0
Thankyou ideiamais

I saw a post this morning talking about using the 4.1 API because the current API wasnt working well but I thought to myself that at some point when they deprecate the 4.1 API I'd need to rewrite the code so I thought it was worthwhile pursuing the v8 system as Im hoping that once I can get a token then Im hoping the rest will work okay. If it turns out that it wont work with our configuration, then your example, coupled hopefully with some code online to figure how to do searches for accounts after a set date would likely set me up for what I need to do, so thank you very much for the example. Your code is now my fallback position!

Many thanks !

Tony
The administrator has disabled public write access.

API Request 9 months 1 week ago #70382

  • fanton.ff
  • fanton.ff's Avatar
  • Offline
  • Senior Member
  • Posts: 68
  • Thank you received: 6
  • Karma: 5
Ok you confirmed that SuiteCRM is getting the wrong path :) It should print "oauth/access_token" instead of "/". I fear the following is NOT the Right Way to fix this, but can you try patching lib/API/core/app.php and replace
$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
with
$_SERVER['REQUEST_URI'] = $_SERVER['QUERY_STRING'];

(I had to do this to use SuiteCRM on Nginx)
The administrator has disabled public write access.
The following user(s) said Thank You: kevbe
Time to create page: 0.065 seconds
Powered by Kunena Forum