Fortinet Forum
The Forums are a place to find answers on a range of Fortinet products from peers and product experts.
xxddpac
New Contributor

Python for Fortigate API

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 ?

5 REPLIES 5
xxddpac
New Contributor

Any ideas ?

pcraponi

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

 

maybe can help you..

Regards, Paulo Raponi

xxddpac

p****oni wrote:

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

 

maybe can help you..

Many thanks !!

jamerst26
New Contributor

You have Reference document

rpereira

I had similar problem (error 401).

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

Confirm the url sent with res.url.