Forum Discussion

pranabpal's avatar
pranabpal
New Contributor
6 years ago

token generation using swagger

using 

Curl command which generates the above token is :

 

curl -X POST "https://login.microsoftonline.com/ada8448e-faae-4047-99a6-15a17f4db850/oauth2/token" -H "accept: application/json" -H "Subscription_Key: e1140ee2416d442cbd3c5793d453fe73" -H "Content-Type: multipart/form-data" -F "grant_type=Client_credentials" -F "client_id=fd199a1e-1851-43a7-9fbf-fcdde1cdecbf" -F "client_secret=lGNcdjpdcw0a0c/l41AsFYICakC1B56a9DGzQ7vfWnk="

 

output is:

 

"token_type": "Bearer",
"expires_in": "3600",
"ext_expires_in": "3600",
"expires_on": "1548321480",
"not_before": "1548317580",
"resource": "00000002-0000-0000-c000-000000000000",
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Im5iQ3dXMTF3M1hrQi14VWFYd0tSU0xqTUhHUSIsImtpZCI6Im5iQ3dXMTF3M1hrQi14VWFYd0tSU0xqTUhHUSJ9.eyJhdWQiOiIwMDAwMDAwMi0wMDAwLTAwMDAtYzAwMC0wMDAwMDAwMDAwMDAiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC9hZGE4NDQ4ZS1mYWFlLTQwNDctOTlhNi0xNWExN2Y0ZGI4NTAvIiwiaWF0IjoxNTQ4MzE3NTgwLCJuYmYiOjE1NDgzMTc1ODAsImV4cCI6MTU0ODMyMTQ4MCwiYWlvIjoiNDJKZ1lGaFdZRnQzZnhXanpGM1JsY3RTcDV6MkJnQT0iLCJhcHBpZCI6ImZkMTk5YTFlLTE4NTEtNDNhNy05ZmJmLWZjZGRlMWNkZWNiZiIsImFwcGlkYWNyIjoiMSIsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2FkYTg0NDhlLWZhYWUtNDA0Ny05OWE2LTE1YTE3ZjRkYjg1MC8iLCJvaWQiOiJjMjkzNTE3Yy0zZGFlLTQ3MGEtOWNhYy0yZTRiYThlZmUwZTAiLCJzdWIiOiJjMjkzNTE3Yy0zZGFlLTQ3MGEtOWNhYy0yZTRiYThlZmUwZTAiLCJ0ZW5hbnRfcmVnaW9uX3Njb3BlIjoiTkEiLCJ0aWQiOiJhZGE4NDQ4ZS1mYWFlLTQwNDctOTlhNi0xNWExN2Y0ZGI4NTAiLCJ1dGkiOiJVUEdqbzQzVFNFT3ZaQmw5S1oxYkFBIiwidmVyIjoiMS4wIn0.pZw-roudVpT2rrWTecJHdmMeitpZvKVAC2KlNkL5hRgQJ4T2ehWbj4Yck3dRt_MSP_0Bpqragszvgh6LCkNne6RqnZALujP5jREhizhKLfpM9RL1yOPMZJ3BOSxrZW0WNQJPlMUwQSwJS3_LXcqNsC0JUuvkFEQuCXLTuVtDNrLAW2oRIJ89tD7eIST9lIqER7Iui1ri1LvFrbkAa_6HxHVaJQXXK4rlOElx-ePj3LowrBIt3lrVNtTRUb_1xuRN-H5FA-fsakhRqv5wEgPi1wkrFFMddERMEFYuYyggK35GjXJxg8mvlMEk7hKw6bXAs4JrJVx5hQJDh30DVy4gpQ

But using this swagger file in swagger hub - i cannot able to generate the above token. May I know the reason

 

swagger: '2.0'
info:
  description: null
  version: 1.0.0
  title: null
  termsOfService: null
  contact: null
  license: null
host: login.microsoftonline.com
basePath: ''
schemes:
  - https
paths:
  /ada8448e-faae-4047-99a6-15a17f4db850/oauth2/token:
    post:
      tags:
        - test10
      summary: null
      description: null
      operationId: test10
      produces:
        - application/json
      consumes:
        - multipart/form-data
      parameters:
        - name: Subscription_Key
          in: header
          description: null
          required: false
          type: string
        - name: grant_type
          in: formData
          description: null
          required: false
          type: string
        - name: client_id
          in: formData
          description: null
          required: false
          type: string
        - name: client_secret
          in: formData
          description: null
          required: false
          type: string
      responses:
        '200':
          description: successful operation
          content:
            application/json:
          schema:
            $ref: '#/definitions/test10'
definitions:
  test10:
    properties:
      token_type:
        type: string
      expires_in:
        type: number
        format: int32
      ext_expires_in:
        type: number
        format: int32
      expires_one:
        type: number
        format: int32
      not_before:
        type: number
        format: int32
      resource:
        type: string
      access_token:
        type: string

 

 

 

3 Replies

  • Hello,

     

    There are several issues here.

     

    First, your API definition is invalid, and SwaggerHub should show you were. Generally speaking, `null` is not a valid value, so anything with that value would need to be either erased or given a valid value.

     

    The second part is that you're trying to describe an OAuth2 security process using direct API paths, which is not the right approach with the OpenAPI Specification. You can find out more about how to define OAuth2 in your definitions through https://swagger.io/docs/specification/2-0/authentication.

    • pranabpal's avatar
      pranabpal
      New Contributor

      Can you help me if you can modify the swagger so that it become workable. I am very novice in writing swagger script if you can write for me - i will be grateful

    • pranabpal's avatar
      pranabpal
      New Contributor

      from this code i cannot able to create json though this code is valid in swaggerhub

       

      {
        "swagger" : "2.0",
        "info" : {
          "description" : "",
          "version" : "1.0.0",
          "title" : "",
          "termsOfService" : "",
          "contact" : "",
          "license" :""
        },
        "host" : "login.microsoftonline.com",
         "basePath":"\/",
         "responses":{
                     "200":{
                        "description":"OK",
                        "schema":{
                           "title":"ResponseOfIdentity",
                           "type":"object",
                           "properties":{
                              "access_token":{
                                 "type":"string"
                              },
                              "scope":{
                                 "type":"string"
                              },
                              "expires_in":{
                                 "type":"integer"
                              },
                              "token_type":{
                                 "type":"string",
                                 "enum":[
                                    "bearer"
                                 ]
                              }
                           }
                        }
                     }
                  },
            "tags":[
            {
               "name":"test10",
            }
         ],
         "schemes" : [ "https" ],
        "paths" : {
          "/ada8448e-faae-4047-99a6-15a17f4db850/oauth2/token" : {
            "post" : {
              "tags" : [ "test10" ],
              "summary" : null,
              "description" : null,
              "operationId" : "test10",
              "produces" : [ "application/json" ],
              "consumes" : [ "application/x-www-form-urlencoded" ],
              "parameters" : [ {
                "name" : "Subscription_Key",
                "in" : "header",
                "description" : null,
                "required" : false,
                "type" : "string"
              }, {
                "name" : "grant_type",
                "in" : "formData",
                "description" : null,
                "required" : false,
                "type" : "string"
              }, {
                "name" : "client_id",
                "in" : "formData",
                "description" : null,
                "required" : false,
                "type" : "string"
              }, {
                "name" : "client_secret",
                "in" : "formData",
                "description" : null,
                "required" : false,
                "type" : "string"
              }],
              "responses":{
                     "200":{
                        "description":"OK",
                        "schema" : {
                    "$ref" : "#/definitions/test10"
                     }
                  }
            }
          }
        }
        },
        "definitions" : {
          "test10" : {
            "type" : "object",
            "properties" : {
              "token_type" : {
                "type" : "string"
              },
              "access_token" : {
                "type" : "string"
              }
            }
          }
        }
        }

       

      code is valid in swaggerhub