Newsletter2go ruby API client authorisation with get_token method

84 views Asked by At

Good day! Can you please give me an example of authentication request with ruby newsletetr2go API client. I can't figure it out. I can connect to API using direct requests like RestClient.post "#{link}/oauth/v2/token", credentials, default_header In credentials I use my username, password and grant_type, converted to json format

In default header I use content_type: 'application/json' and authorization: "Basic #{Base64.strict_encode64(ENV['NEWSLETTER2GO_AUTH_KEY'])}" And it works fine. But when I try to use newsletter2go get_token method all I receive is "BAD REQUEST" error.

I'm using initializer to configure SwaggerClient like this:

SwaggerClient.configure do |config|
  # Configure OAuth2 access token for authorization: OAuth
  config.password   = ENV['NEWSLETTER2GO_PASSWORD']
  config.username   = ENV['NEWSLETTER2GO_USERNAME']
  config.api_key    = ENV['NEWSLETTER2GO_AUTH_KEY']
end

After that I use newsletter2go api method call

SwaggerClient::AuthorizationApi.new.get_token("https://nl2go.com/jwt")

Seems everything is correct, but error "BAD REQUEST" happens all the time.

I followed the instructions, install swagger_client with ruby extentions in github, and newsletter2go methods are now available from my rails environment. If I grab access_token manually and add it to my initializer, then do some requests like SwaggerClient::ListApi.new.get_lists it gives me a proper response with status 200 and list_ids

But SwaggerClient::AuthorizationApi.new.get_token("https://nl2go.com/jwt") does not work and this is the issue. Any help would be very appreciated!

1

There are 1 answers

0
Kirill Zhuravlov On BEST ANSWER

I figured out the reason why Newsletter2Go API ruby client does not grab api_key value. For some reason it's hardcoded to setup basic auth token which stands from username and password packed. Here is code from

module SwaggerClient class Configuration

# Gets Basic Auth token string
def basic_auth_token
  'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
end

# Returns Auth Settings hash for api client.
def auth_settings
  {
    'OAuth' =>
      {
        type: 'oauth2',
        in: 'header',
        key: 'Authorization',
        value: "Bearer #{access_token}"
      },
    'Basic' =>
      {
        type: 'basic',
        in: 'header',
        key: 'Authorization',
        value: basic_auth_token
      },
  }
end