Getting permission error with REST API

I am trying to use your REST API to create leads in erp.
I am logged in successfully… but facing problem in creating any document ie lead

Is there any API key or some process to get permission to create document in erpnext?

CODE:

<?php //******************** LOGIN REQUEST ********************************// $url= 'http://128.199.200.206/api/method/login'; // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS,array( 'usr'=>'vineet@mittalagro.com', 'pwd'=>'vineet1234' // it is not real passwd but while sending request i am using real paswd )); // Execute post $result = curl_exec($ch); // Close connection curl_close($ch); //******************** DOCUMENT CREATE REQUEST ******************************// $response= json_decode($result,true); if($response['message']=='Logged In'){ // Create lead.................................// $url = 'http://128.199.200.206/api/resource/Lead'; // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS,array( 'data' => '{"lead_name":"Lead00011"}' )); // Execute post $result = curl_exec($ch); // Close connection curl_close($ch); echo $result; } ?>

It doesn’t look like you are passing a cookie from the log in sequence when you are trying to create the document. I think the system is not recognizing that the person that logged in is trying to create the document.

1 Like

Yes its not recognizing. but how to pass cookie from the log in sequence?
Nothing mentioned in documentation of Rest API that how to tell ERP System that who is trying to create document.

The user with which you login is the user that creates the document.

Yes that I know but to tell erpnext that this usr is creating document I need to passing a cookie from the log in sequence while trying to create the document.

So my question is how to pass a cookie from the log in sequence while creating document?

Every API like linkedin, facebook, google etc have api key and secret ki… just by passing those key along with the request help their system to validate our request.

But i think here things are different.
Please provide proper information that how to create documents? what steps are involve into it?
I know it’s simple but don’t have proper information about it.

As you can see, I am trying to do it from last 4 days. need immediate help.

this is a pretty standard thing. You pass cookies in your request headers.

See: GitHub - frappe/frappe-client: Python library to use Frappe API

In PHP: GitHub - tmimori/How-to-connect-to-ERPNext-v5-API-with-PHP5: How to connect to ERPNext v5 API with PHP 5

1 Like
  1. Did configuration file settings with correct email & password

‘auth_url’ => ‘Frappe Cloud’,
‘api_url’ => ‘Frappe Cloud’,
‘auth’ => array(‘usr’ => ‘vineet@mittalagro.com’, ‘pwd’ => ‘#######’),
‘cookie_file’ => ‘cookie.txt’,
‘curl_timeout’ => 30,

  1. Put all related files in same directory. View attachment
  2. Set const CONF_FILE = ‘config.php’;

Still getting Auth fail;
check Url: WholesaleRaja.com is for sale | HugeDomains

@vineet_saraogi no experience with PHP, I recommend you should engage a freelancer for this.

Hi rmehta,

You provided me php file link, I did whatever required. PHP is a very simple language, any developer can easily understand the code. I did whatever mentioned there. I am having good development experience in PHP. So even after doing everything as instructed, still it’s not validating. You give it a try, its just 5 min work. Because you know your system better.

@vineet_saraogi share the full trace.

changes need to make only in these two files…

SAMPLE.PHP

<?php /** * FrappeClient-PHP Sample Script * */ require(dirname(__FILE__).'/FrappeClient.php'); try{ $client = new FrappeClient(); var_dump($client); // get ---------------------------- $result = $client->get( "User" ,"puneetverma@mittalclothing.com" ); var_dump($result); }catch(Exception $e){ var_dump($e); } set_error_handler(function ($errno, $errstr, $errfile, $errline ) { throw new ErrorException($errstr, 0, $errno, $errfile, $errline); }); CONFIG.PHP <?php if(!defined("ROOTPATH")){die("Access forbidden.".PHP_EOL);} return array( 'auth_url' => 'http://mittalclothing.erpnext.com/api/method/login', 'api_url' => 'http://mittalclothing.erpnext.com/api/resource/', 'auth' => array('usr' => 'vineet@mittalagro.com', 'pwd' => 'password of this user'), 'cookie_file' => 'cookie.txt', 'curl_timeout' => 30, 'basic_auth' => array(), // 'basic_auth' => array('usr' => '', 'pwd' => ''), ); This much only need to change.. there is no other setting... So now we can test with link http://erp.wholesaleraja.com/erpnext/sample.php

ok one more thing… in FrappeClient.php

I did necessary change,
const CONF_FILE = ‘/var/www/erpnext/config.php’; to const CONF_FILE = ‘config.php’;

I think this may solve…
Set specified User for using API on ERPNext

How to do this in ERPNext ???

i done same but i got error “Auth fail”
‘auth_url’ => ‘Frappe Cloud’,
‘api_url’ => ‘Frappe Cloud’,
‘auth’ => array(‘usr’ => ‘amolvision@outlook.com’, ‘pwd’ => ‘#######’),
‘cookie_file’ => ‘cookie.txt’,
‘curl_timeout’ => 30,

‘basic_auth’ => array(),