How to connect to a remote web service that provides tokens


#1

I am trying to access a remote rest api that generates a token after providing it with an app id and secret key. How do I add it to dreamfactory. I tried going over the edmunds api tutorial but I dont think it covers what I am trying to achieve.


#2

Hi @mymanga,

I think edmunds api tutorial is a good start point, lets build a example:

First you need to create a new service, type: Remote Service

Go to the tab Config, insert the remote server URL and create the Parameters you want to send from your client to the remore server, check Outbound and all the Verbs

Then paste this json in Service Definition tab, do this and your service will be displayed in API Docs:

{
  "swagger": "2.0",
  "info": {
    "version": "2.0",
    "title": "Remote WS Example",
    "description": "Remote ws example."
  },
  "paths": {
    "/remotews": {
      "post": {
        "tags": [
          "remotews"
        ],
        "description": "Get token.",
        "parameters": [
          {
            "name": "app_id",
            "in": "formData",
            "description": "application id",
            "type": "string",
            "required": true
          },
          {
            "name": "secret_key",
            "in": "formData",
            "description": "my secret key",
            "type": "string",
            "required": true
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "$ref": "#/definitions/TokenSuccess"
            }
          },
          "default": {
            "description": "Error",
            "schema": {
              "$ref": "#/definitions/TokenError"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "TokenSuccess": {
      "type": "object",
      "properties": {
        "token": {
          "type": "string"
        }
      }
    },
    "TokenError": {
      "type": "object",
      "properties": {
        "code": {
          "type": "integer",
          "format": "int32",
          "description": "Error code."
        },
        "message": {
          "type": "string",
          "description": "String description of the error."
        }
      }
    }
  },
  "parameters": {
    "app_id": {
      "name": "app_id",
      "in": "formData",
      "description": "application id",
      "required": true,
      "type": "string"
    },
    "secret_key": {
      "name": "secret_key",
      "in": "formData",
      "description": "my secret key",
      "required": true,
      "type": "string"
    }
  }
}

Go to the API Docs, find your service, complete the fields and hit the button Try it out:

Hope it helps you :wink:


#3

Thanks but getting error:

“405 Method not allowed”

It seems to only accept GET requests

When you run it in the browser

http://api.example.com/token/?appid=q1w2e3&key=1qazxsw23edc

It returns;

{
“status”: “SUCCESS”,
“token”: “b9abb385c2f33434a3586295044e508d”
}

When I change the request to GET in the Service Definition, response on API DOCS is;

“405 Content in response can not be resolved to acceptable content type.”


#4

No problem, change the json in service definition to this:

 {
  "swagger": "2.0",
  "info": {
    "version": "2.0",
    "title": "Remote WS Example",
    "description": "Remote ws example."
  },
  "paths": {
    "/remotews": {
      "get": {
        "tags": [
          "remotews"
        ],
        "description": "Get token.",
        "parameters": [
          {
            "name": "appid",
            "in": "query",
            "description": "application id",
            "type": "string",
            "required": true
          },
          {
            "name": "key",
            "in": "query",
            "description": "my secret key",
            "type": "string",
            "required": true
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "$ref": "#/definitions/TokenSuccess"
            }
          },
          "default": {
            "description": "Error",
            "schema": {
              "$ref": "#/definitions/TokenError"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "TokenSuccess": {
      "type": "object",
      "properties": {
        "token": {
          "type": "string"
        }
      }
    },
    "TokenError": {
      "type": "object",
      "properties": {
        "code": {
          "type": "integer",
          "format": "int32",
          "description": "Error code."
        },
        "message": {
          "type": "string",
          "description": "String description of the error."
        }
      }
    }
  },
  "parameters": {
    "app_id": {
      "name": "appid",
      "in": "query",
      "description": "application id",
      "required": true,
      "type": "string"
    },
    "secret_key": {
      "name": "key",
      "in": "query",
      "description": "my secret key",
      "required": true,
      "type": "string"
    }
  }
}

I’m assuming that your Remote Service has the parameters appid and key


#5

First hurdle…crossed/jumped/ :slight_smile: . Seeing how the url is meant only get a session token, how do I use that token to make subsequent requests to consume the api seeing how

token is:

http://api.example.com/token/?appid=q1w2e3&key=1qazxsw23edc

data is;

http://api.example.com/cows
http://api.example.com/goats

Can all this goes into one Remote Web service??


#6

You can config the Remote Service (remotews) with you base url (http://api.example.com/), when you call the dreamfactory just append the endpoint (http://your_dreamfactory/api/v2/remotews/cows, http://your_dreamfactory/api/v2/remotews/gosts or http://your_dreamfactory/api/v2/remotews/token)

If you are not developing some APP to consume dreamfactory services, you will need to make the calls one by one, changing the params.
OR
Create a Custom Script (read this too) to call the remote api, get the token, insert the token in a new request and return the response.

For testing, you can use Postman (app for chrome) , if u dont know, you will like. :slight_smile:

Cheers


#7

Thank you. lemme work on that. Will update on progress. Thank you for the awesome support


#8

Good luck with your work! I’m glad to help :slight_smile: