0

I am trying to read a csv attachment using python requests.get method with oAUTH token. Its working fine in postman but not from from my program. I am trying to use below code

import requests

def callF():
    url = "https://jiraurl.com/secure/attachment/26433/Bulk_Repo_Archival_SMDH.csv"
    token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkpKRmRuSURYTngt"
    jira_headers = {"Authorization":str(token)}
    #print(jira_headers)
    data = requests.get(url,headers= jira_headers,verify=False)
    print(data.content)
   
    
if __name__ == '__main__':  
    callF()

I am getting below response:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
    <body onload="document.forms[0].submit()">
        <noscript>
            <p>
                <strong>Note:</strong> Since your browser does not support JavaScript,
                you must press the Continue button once to proceed.
            </p>
        </noscript>
        <form action="https://idag2.jpmorganchase.com/adfs/ls/" method="post">
            <div>
                <input type="hidden" name="RelayState" value="-ZIP8P-/secure/attachment/2226433/Bulk_Repo_Archival_SMDH.csv"/>
                <input type="hidden" name="SAMLRequest" value="PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c2FtbDJwOkF1dGhuUmVxdWVzdCB4bWxuczpzYW1sMnA9InVybjpvYXNpczpuYW1lczp0Yz"/>
            </div>
            <noscript>
                <div>
                    <input type="submit" value="Continue"/>
                </div>
            </noscript>
        </form>
    </body>
</html>

However in postman also it gave same response for me for sometime but later somehow it is giving proper csv file data. I tried checking in multiple places but no luck. FYI it was working fine when we used basic authentication but after shifting to oAUTH its not working. Please help.

  • Obviously we can't try this as that's not a real URL. However, try passing a User-Agent in your headers - that might help – DarkKnight May 20 '22 at 15:05

1 Answers1

0

I am able to resolve this by using below code:

from jira import JIRA
from jira_oauth import get_access_token

username = 'A12345'
jira_access_token = get_access_token()

download_folder = "downloads/"
server =  "https://jira.prod.aws.net"

jira = JIRA(basic_auth=(username, jira_access_token), options={'server': server,'verify': False})
issue = jira.issue('DEVSERVOPS-11111', fields='summary,comment,attachment')

for attachment in issue.fields.attachment:
    with open(download_folder + '%s' % (attachment.filename), 'wb') as file:
        file.write(attachment.get())