Skip to main content
xxddpac
New Member
November 28, 2018
Question

Python for Fortigate API

  • November 28, 2018
  • 2 replies
  • 22097 views

Hi,Guys

I have a problem with FGT60E API token.

The python code is as follows:

========================================================================

========================================================================

import json,urllib3,requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
url = "https://X.X.X.X:4433"
fgt_url = "https://X.X.X.X:4433/logincheck"
# fgt_coo = requests.post(fgt_url,data={"username":"aaaaa","secretkey":"aaaaaaa",
# "ajax":1},verify = False)
# headers = {"Cookie":"APSCOOKIE_2405464622=%s;ccsrftoken=%s"%(fgt_coo.cookies.get("APSCOOKIE_2405464622"),
# fgt_coo.cookies.get("ccsrftoken"))}
myToken = "9qcGftQshfk9Hhqp9Gzxxx7rd14djg"
apikey = {'Authorization': 'python {}'.format(myToken)}
cmdb = url + "/api/v2/cmdb"
res = requests.get(url = cmdb,headers = apikey,verify = False)
print res.content
# result = json.loads(res.content)
# print result["version"]
# print result["serial"]
================================================================================
================================================================================
The result :

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>401 Authorization Required</TITLE> </HEAD><BODY> <H1>Authorization Required</H1> This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.<P> <P>Additionally, a 401 Authorization Required error was encountered while trying to use an ErrorDocument to handle the request. </BODY></HTML>

 

The Firewall already created API User "python" and generate key.

I used cookie its ok, but api key failed.

Can anyone help me ?

    2 replies

    xxddpac
    xxddpacAuthor
    New Member
    November 29, 2018

    Any ideas ?

    pcraponi
    New Member
    November 29, 2018

    Take a look here in some examples: https://github.com/barbosm/gatepy

     

    maybe can help you..

    xxddpac
    xxddpacAuthor
    New Member
    November 30, 2018

    p****oni wrote:

    Take a look here in some examples: https://github.com/barbosm/gatepy

     

    maybe can help you..

    Many thanks !!

    jamerst26
    New Member
    February 5, 2020

    You have Reference document

    rpereira
    New Member
    July 30, 2020

    I had similar problem (error 401).

    In my case, the Token had an extra '\n'.

    Confirm the url sent with res.url.