Forum Discussion

irach15's avatar
irach15
Maven
5 years ago

cUrl call to get data

hey,

sorry for simple questions to all API gurus 😉

I just started using cUrl for getting API data, somehow after working years as Admin and Dev never has been needed...

Being said, I want to understand how I as Admin can make a simple cUrl call to get for example all data for one user with a specific user ID.

I see all the sample under Dev docs,

but still having a situation or THE situation to code a cUrl call.

Questions:

- do I need a session-key to Get call?

- how to get a session-key?

- can I use instead of session-key another authentication method  or none because I'm an Admin and signed in at the moment of the call?

for example to get all badges from a user:

curl -X GET \
    'https://[COMMUNITY-DOMAIN]/api/2.0/users/13/badges/' \
    -H 'Authorization: Bearer [TOKEN]' \
    -H 'client-id: [CLIENT-ID]'

I got: client-id, but is Bearer [TOKEN]?

another example, for this one, how to code a curl call?

select id, name, node.id, role_status from roles where users.id = '17'

Trying to see the logic and understand how to make a call.

20 Replies

  • (I started writing this reply before Stan's response, but forgot to submit. Hopefully still useful).

    I also struggled with this a lot when playing around w/ API.

    I ended up:

    (1) creating a NON-SSO user via the admin console (Users > Permissions > Create User) & granting them API permissions

    (2) Using Postman to handle the session keys. Unfortunately I don't remember the details of how I did this, but once it was set up it was nice and easy to make calls. Check out  https://www.postman.com/ 

    Cheers!

  • TedV's avatar
    TedV
    Khoros Alumni (Retired)
    5 years ago

    Hi irach15 ,

     

    If any of the comments answered your question, would you mind marking it as an Accepted Solution?

    Thanks!

  • TedV & SuzieH 

    updates:

    I've tried as recommended. 

    - created non-SSO account

    - granted Admin role

    tried to get a session key from Suzie's example, no luck

    <response status="error">
      <error code="302">
        <message>
          User authentication failed.
        </message>
      </error>
    </response>
    curl: (6) Could not resolve host: application
    'user.password' is not recognized as an internal or external command,
    operable program or batch file.

    Note: I'm on Windows 10. cUrl installed by default and it's running.

    Any advice on this?

    I've read lots of docs on cUrl...

  • irach15's avatar
    irach15
    Maven
    5 years ago

    CarolineS 

    tried

    - non-SSO user created

    - signed on to Postman

    - run Post request with user name & psw

    - no luck

    <response status="error">
        <error code="302">
            <message>
          User authentication failed.
        </message>
        </error>
    </response>

     

  • CarolineS's avatar
    CarolineS
    Boss
    5 years ago

    Hi irach15 -

    I am far far FAR from an expert on this, but here is what I did. 

    (1) Call /authentication/sessions/login to get a session key: 

    (2) Once you get that session key, save it in the Headers for your next request:

    Hope that helps!

  • irach15's avatar
    irach15
    Maven
    5 years ago

    CarolineS 

    aha... I see your url is different than provided in all Khoros docs...

    I'll try.

    Can you pz past the Post url here?

    also, what is Authorization in Postman, basic?

    Postman settings will help a lot to 🙂

    thanks.

  • CarolineS's avatar
    CarolineS
    Boss
    5 years ago

    I followed the instructions for Session Key authentication, here: https://developer.khoros.com/khoroscommunitydevdocs/docs/session-key

    Per that documentation, the URL for authentication is: https://[COMMUNITY-DOMAIN]/restapi/vc/authentication/sessions/login (w/ parameters user.login and user.password)

    I didn't change the Postman settings from the default settings at all - I see that the Authorization settings are set to: Type: 0Auth 1.0 and Signature Method: HMAC-SHA1 - again, pretty sure that was just what the defaults were.

    Hope this helps! This is pretty much the extent of my knowledge on this so hopefully someone else will have answers if you have further questions!

    Cheers!

  • Another approach is to use an Khoros Endpoint.

    You can use POSTMAN to issue a http request with a "API-TOKEN" header to poke this endpoint.

    In the endpoint, you can check this API-TOKEN. Then if API-TOKEN check is correct, then you can write a restadmin call to get whatever data you need. Also you can use http.client object to do a CURL out to 3rd party system.

    This method does not require a user to be created.

  • irach15's avatar
    irach15
    Maven
    5 years ago

    peterlu 

    and I need to code an endpoint for the call?

    why is it so complicated? it should be just easy call to check and get the data...

    any sample for endpoint call appreciated

    thanks

  • peterlu's avatar
    peterlu
    Champion
    5 years ago

    irach15 Here is some coding example. Being a developer is not fun 🙂 I can share your pain.

     

     

    <#compress>
    <#assign header_names = http.request.headerNames />
    <#assign token_from_request = "" />
    <#if header_names?seq_contains("API-TOKEN")>
      <#assign token_from_request = http.request.getHeader("API-TOKEN") />
    </#if>
    
    <#assign token_from_setting = community.settings.name.get("your.community.settings.list.variable", "") />
    
    <#if token_from_request == token_from_setting>
      <#assign query = "........." />
      <#assign items = restadmin("2.0", "/search?q=${query?url}").data.items![] />
      {
        "data": [
          <#list items as item>
            {
            	// some json print out coding
            }
          </#list>
        ]
      }
    <#else>
      {"error": "invalid request"}
    </#if>
    </#compress>

     

     

     

    <#compress>
    <#assign header_names = http.request.headerNames />
    <#assign token_from_request = "" />
    <#if header_names?seq_contains("API-TOKEN")>
      <#assign token_from_request = http.request.getHeader("API-TOKEN") />
    </#if>
    
    <#assign token_from_setting = community.settings.name.get("your.community.settings.list.variable", "") />
    
    <#if token_from_request == token_from_setting>
      <#-- curl out to 3rd party -->
      <#assign response = http.client.request("some url").header("Content-Type","application/x-www-form-urlencoded").parameter("p1", "v1").parameter("p2", "v2").post()/>
      <#--some error condition checks-->
      <#--success or error JSON print out-->
    <#else>
      {"error": "invalid request"}
    </#if>
    </#compress>