6 Replies Latest reply on Mar 17, 2019 4:37 AM by rsingh13

    vRNI API

    rsingh13 Lurker

      Hi Members

       

      I am using vRNI api calls for listing the available data sources. I can view/add data sources using GUI but I cant do it with API call. I am getting {"code":401,"message":"HTTP 401 Unauthorized"} in response?

      Any help?

      Thanks

      Rsingh13

        • 1. Re: vRNI API
          smitmartijn Hot Shot
          vExpertVMware Employees

          Hi,

           

          What is the exact API call you're executing? Are you logged in via /api/ni/auth/token and then using the returned authentication token in the headers for the data source API call?

          • 2. Re: vRNI API
            rsingh13 Lurker

            smitmartijn

             

            Yes, here is the code that I wrote for this.

             

             

            import requests, os

            import json

             

             

            def GenrateToken():

               

                BaseUrl = "https://vrniHost.corp/api/ni/auth/token"

                creds = {

                "username": os.environ.get('vrni_user'),

                "password" : os.environ.get('A_PWD'),

                "domain": {

                    "domain_type": "LDAP",

                    "value":"corp"

                    }

                }

                headers = {'Content-Type':'application/json'}

                resp =  requests.post(BaseUrl,headers=headers, data = json.dumps(creds), verify = False,)

                key = resp.json()['token']

                print key

                return key

             

             

             

            def GetSwitch(key):

                AuthKey = key

                BaseUrl = "https://vrniHost.corp/api/ni"

                Uri = "/data-sources/cisco-switches"

                headers = {

                "Content-Type":"application/json",

                "Authorization":AuthKey

                }

                CiscoResp = requests.get((BaseUrl+Uri),headers = headers, verify=False)

                print CiscoResp.text

             

             

            key = GenrateToken()

             

            GetSwitch(key)

            • 3. Re: vRNI API
              smitmartijn Hot Shot
              vExpertVMware Employees

              Try this, the correct header is: Authorization: NetworkInsight AuthKey

               

               

              • 4. Re: vRNI API
                rsingh13 Lurker

                Do you mean it should

                 

                headers = {

                    "Content-Type":"application/json",

                    "NetworkInsight AuthKey":AuthKey

                    }

                 

                ?

                • 5. Re: vRNI API
                  smitmartijn Hot Shot
                  VMware EmployeesvExpert

                  No. It's exactly as I mentioned, the header should be: "Authorization: NetworkInsight YourAuthenticationToken"

                   

                  This might translate to the following code, but I'm not sure if the syntax is correct.

                   

                  headers = {

                      "Content-Type":"application/json",

                      "Authorization": "NetworkInsight " AuthKey

                      }

                  • 6. Re: vRNI API
                    rsingh13 Lurker

                    This worked, Thank you very much.

                     

                    Is there a document that helps with these small details? Unfotunately I couln't find anything by myself and api explorer doesn't provide this.