CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • figuring out the REST interface
Pages: [1]

Author Topic: figuring out the REST interface  (Read 1835 times)

sheldon

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
figuring out the REST interface
September 28, 2011, 01:51:57 pm
Hi, I'd appreciate any help in figuring out how to properly access the REST interface.

CiviCRM version 4.0.4 with Drupal

When I visit my API explorer at civicrm/ajax/doc#explorer and generate a contact get REST query it looks like this:
.../sites/all/modules/civicrm//extern/rest.php?json=1&debug=1&version=3&entity=Contact&action=get&user={youruser}&pwd={password}&key={yourkey}

Unfortunately when I try to call this (where key is my site key found in civicrm.settings.php and user/pwd correspond to my Drupal user) I receive:
{"error_message":"Unknown function invocation.","is_error":1}

I've figured out that modify the query to the following is the only way to make it work (where api_key is what I generated and put into the civicrm_contacts table):
.../sites/all/modules/civicrm/extern/rest.php?q=civicrm/contact/get&key={yourkey}&json=1&api_key={your api_key}

Will successfully return the contact, but if I call this query without the api_key param the server will crash (making api_key the only option for authentication).

Unfortunately this is as far as I've been able to get.

If I try to do an update call such as this:
.../sites/all/modules/civicrm/extern/rest.php?q=civicrm/contact/update&id=26504&contact_type=Individual&do_not_email=1&key={yourkey}&json=1&api_key={your api_key}

I receive:
{"is_error":1,"error_message":"API permission check failed for civicrm_contact_update call; missing permission: add contacts."}

However, I've given the Drupal user corresponding to my api_key an administration role that has every single permission, including add contacts.

Am I going about this wrong?



Any tips would be appreciated, thanks!

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: figuring out the REST interface
September 28, 2011, 01:53:57 pm
Does switching to 'create' rather than 'update' change anything?
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

sheldon

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
Re: figuring out the REST interface
September 28, 2011, 01:57:20 pm
Thanks for the reply, unfortunately I receive a very similar error using create:
{"is_error":1,"error_message":"API permission check failed for civicrm_contact_create call; missing permission: add contacts."}

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: figuring out the REST interface
September 28, 2011, 01:59:06 pm
Hi,

I think the authentication for rest is an issue (we need to move to oAuth). In the meantime, I would suggest to use api_key + key.

I will update the ajax explorer accordingly.


BTW, you can copy class.api.php in the client project.

You can then do


Code: [Select]
  $api = new civicrm_api3 (array ('server' => 'http://example.org','api_key'=>'theusersecretkey','key'=>'thesitesecretkey'));
$api->Contact->get ($params);

$api->values() contains the contacts.



Check out the example code in this file. be sure to use the latest version, it has fixed a few bugs...
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: figuring out the REST interface
September 28, 2011, 01:59:51 pm
Quote from: sheldon on September 28, 2011, 01:57:20 pm
Thanks for the reply, unfortunately I receive a very similar error using create:
{"is_error":1,"error_message":"API permission check failed for civicrm_contact_create call; missing permission: add contacts."}

The contact user having the api_key needs the proper permissions. Are you really really sure it's the case?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

sheldon

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
Re: figuring out the REST interface
September 28, 2011, 02:02:46 pm
Thanks for the reply xavier, my client is a Rails server so the PHP code won't help unfortunately.

I've given the civicrm_contact the api_key and the corresponding Drupal user has administrator permissions (every single permission), is there anything else I need to setup?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: figuring out the REST interface
September 28, 2011, 02:28:39 pm
Hi,

Would be cool if you can publish the ruby code you are going to dev for that. As for the access rights, not sure, you will have to debug.

That's where the permission is tested (for the specific permissions)

/CRM/Core/DAO/.permissions.php

X+

P.S. You might want to upgrade or backport, the REST interface got some attention lately, might help
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

sheldon

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
Re: figuring out the REST interface
September 28, 2011, 03:17:05 pm
Thanks, I don't have direct access to my Civi server so it may take some time before I can do any debugging or upgrades (glad to hear the REST interface is getting attention).

I've merely forked the original civicrm-client-rest-ruby library and made some minor changes to make it "work", it's on github at sheldond/civicrm-client-rest-ruby (I can't post links yet)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: figuring out the REST interface
September 29, 2011, 02:11:03 am
Hi,

Yeap, REST had a lot of various undocumented ways of working, or kind of working.

To clarify where I'm heading to:

/sites/all/modules/civicrm//extern/rest.php?json=1&version=3&entity=Contact&action=get&api_key={api_key}&key={yourkey}

ie params that are mandatory are the key, the api_key, the entity and the action. It should work that way in the latest version, if it doesn't 100% it's a bug.

I'm trying to update the doc/code accordingly. Please keep pointing where it isn't described like that (or better yet, wiki edit ;)

It should work that way for a while. When we we will introduce oAuth later, we will likely put a config switch allowing to force oAuth or to keep that existing scheme.

Btw, if you have experience is good rest apis, please join the Api team mailing list.

X+

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: figuring out the REST interface
October 22, 2011, 03:57:09 pm
Xavier, I'm having a little bit of a problem.

I am trying to create a contact using the REST API.

When I pass this URL:

 http://[site.com]/sites/all/modules/civicrm//extern/rest.php?json=1&debug=1&version=3&entity=Contact&action=create&key=[site_key]&api_key=[admin_user_api_key]&contact_type=Individual&first_name=Test&last_name=Test

I successfully create a user, but the new user is assigned the API key of the admin user I am using.


xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: figuring out the REST interface
October 24, 2011, 06:25:37 am


Quote from: DerekL on October 22, 2011, 03:57:09 pm

 http://[site.com]/sites/all/modules/civicrm//extern/rest.php?json=1&debug=1&version=3&entity=Contact&action=create&key=[site_key]&api_key=[admin_user_api_key]&contact_type=Individual&first_name=Test&last_name=Test

I successfully create a user, but the new user is assigned the API key of the admin user I am using.

Damn!

that's one of these cases where there is a conflict between fields used by the interface and those used by the entities.

Although it won't solve the conflict (we can't) we can at least consider that you can't update/create the api_key from the rest interface.

Would be create if you could unset it between the validation and the time it calls the underlying methods. Probably in there: /civicrm//extern/rest.php

Can you create an issue (patch welcome ;) on the issue tracker?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • figuring out the REST interface

This forum was archived on 2017-11-26.