• Documentation
  • Workato Blog
  • Product Blog
  • Return to Workato
  • Login
  • Automation Institute
  • Documentation
  • Community Forums
New Topic
Discussions Developer Community Connector SDK
O

Oleg Rukavishnikov

started a topic over 4 years ago

Connection in "Connector SDK"

Hi all!
I have the basic authentication using client_id and client_secret instead of username, password. I need to send a post request to get bearer access_token to use it for next API requests. Could you please help me in my code?

  

 connection: {
  fields: [
    { name: "client_id", control_type: "password", optional: false, label: 'Consumer Key' },
    { name: "client_secret", control_type: "password", optional: false, label: 'Consumer Secret' }
  ],

  authorization: {
    type: "oauth2",

    acquire: lambda do |connection|
      response = post("get_token_url").
        payload(
          grant_type: "client_credentials",
          client_id: connection["client_id"]
        ).
        request_format_www_form_urlencoded

        [
          {
            access_token: response["access_token"]
          },
          nil,
          nil
        ]
      end,

	refresh_on: [401, 403],

    refresh: lambda do |connection|
        post("get_token_url").
          payload(
			  grant_type: "client_credentials",
			  client_id: connection["client_id"]
          ).
          request_format_www_form_urlencoded
        end,
  },
 },

  

O

Oleg Rukavishnikov

said over 4 years ago
Get token curl:
curl -X POST \
  get_token_url \
  -H 'Accept: */*' \
  -H 'Authorization: Basic ...' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'client_id=...&grant_type=client_credentials'
Response:
{
    "access_token": "....",
    "expires_in": 3600,
    "token_type": "Bearer"
} 
O

Oleg Rukavishnikov

said over 4 years ago
curl -X GET\
  get_data_url \
  -H 'Authorization: Bearer {access_token}' \
  -H 'Content-Type: application/json' \

Joshua Aaron Navarro

said over 4 years ago

Hi Oleg,


Apologies for the delayed response on this topic.


To authenticate using client ID and secret, you may refer to the following example in the acquire block:


acquire: lambda do |connection|

      response = post("get_token_url").

        payload(

          grant_type: "client_credentials",

          client_id: connection["client_id"]

        ).

        user(connection["client_id"]).

        password(connection["client_secret"]).

        request_format_www_form_urlencoded


        [

          {

            access_token: response["access_token"]

          },

          nil,

          nil

        ]

      end,


The methods .user and .password are the equivalent of appending Authorization: BASIC and <user>:<password> in BASE-64 String encoding in the POST request header. Note that the request must be sent with request_format_www_form_urlencoded.


We can retrieve the access_token from response[access_token]. These parameters are also appended into the original connection object.

You can now use this in the apply block to use on your next API requests.



apply: lambda do |connection, access_token|         
  headers("Authorization":"Bearer #{connection["access_token"]}")      
end



1 person likes this
O

Oleg Rukavishnikov

said over 4 years ago

Hi Joshua,

Could you help me? Now my connection looking like this:

  

connection: {
    fields: [
        { name: "client_id", control_type: "password", optional: false, label: 'Consumer Key' },
        { name: "client_secret", control_type: "password", optional: false, label: 'Consumer Secret' }
      ],

    authorization: {
      type: "oauth2",
      acquire: lambda do |connection|
            response = post("get_token_url").
              payload(
                grant_type: "client_credentials",
                client_id: connection["client_id"]
              ).
              user(connection["client_id"]).
              password(connection["client_secret"]).
              request_format_www_form_urlencoded
              [
                {
                  access_token: response["access_token"]
                },
                nil,
                nil
              ]
            end,    

      refresh_on: [401, 403],

      refresh: lambda do |connection|
         post("get_token_url").
              payload(
                grant_type: "client_credentials",
                client_id: connection["client_id"]
              ).
              user(connection["client_id"]).
              password(connection["client_secret"]).
              request_format_www_form_urlencoded
          end,


      apply: lambda do |connection, access_token|         
              headers("Authorization": "Bearer #{connection["access_token"]}")      
            end
    }
  },

  

But every time I'm getting the next error:
Connection failed: No automatic OAuth refresh available, needs user re-authorization
Or, if I change the type of authentication from oauth2 to basic_auth, I've got the next one:

Connection failed: undefined method `with_indifferent_access' for #<Array:0x00007f51d417b088>


P

Phoo Pwint Aung

said almost 4 years ago

Hello,

       I also try client_credential grant flow (oauth2) of Microsoft graph api in Workato sdk connector.

      But I got this error.

      Connection failed: No automatic OAuth refresh available, needs user re-authorization

How can i fix this error?

 

Best Regards,

Login to post a comment

Still can't find your solution?

Send us a ticket, we will try our best to assist you with your problem

Documentation
Developer's Library
Tutorials
eBooks
Product Hour
Product Blog
Workato Blog
Product updates
Customer Stories
© Workato 2022   Privacy   Terms   +1 (844) 469-6752
  • Documentation
  • Workato Blog
  • Product Blog
  • Solutions
  • Forums
  • Tickets
  • Log in
  • Return to Workato
Topic views count