0

I want to make a rest api call and get access token and that access token will be there for 30 min. Again i need to refresh that token.

I am not getting how to get the access token, tried numerous code snippets.

Sample access token will be as below: KkcwAIyUm6XGbGUA0wejna6_8kk3Zuo66BigYo3gAGI.

URL used in post man to get access token manually: [https://xxx.xxx.com/restapi/vc/authentication/sessions/login?user.login=s_user&user.password=xxx]

Then i need to use this access token in below URL to fetch data: [https://xxx.xxx.com/restapi/vc/messages/id/879997?restapi.session_key=kEf-mTzu6Xnsa5HQKt8ml-9Wc-HX3wHLlphB2oQkrxE.&restapi.response_format=json]

Can anyone help with python code for this

Code i am trying for getting access token:

    import requests
import json

with requests.Session() as session:
    req = requests.get(r'https://xx.xx.com/restapi/vc/authentication/sessions/login?user.login=s_user&user.password=xxxx')

print(req.content)

Output: b'\n F0JQfxkxxwl-O9-cQKtJhdrNRKJg3ENLd_SckmBIqU0.\n\n'

Code working for hard coding access token from post man:

for m in data_list:
    response = requests.get("https://xxx.xx.com/restapi/vc/messages/id/"+m+"?restapi.session_key=kEf-mTzu6Xnsa5HQKt8ml-9Wc-HX3wHLlphB2oQkrxE.&restapi.response_format=json")
    data = response.json()
    cleaned_text=cleanhtml(data['response']['message']['body']['$'])
    body_list.append(cleaned_text)
    #print(cleaned_text)
    #print ('---------BODY ENDS HERE--------')
    tz_subject = data['response']['message']['subject']['$']

When i hard code the access token that i got from post man i am able to fetch results using below code , but i am not getting how to generate access token in python and then use it, refresh it when i expires

Thanks you

13
  • Show the code you have so far Commented Sep 8, 2018 at 0:03
  • Don't put it in the comments but edit your question and format it properly as code Commented Sep 8, 2018 at 0:07
  • @MichaelButscher i have added code in question thank you Commented Sep 8, 2018 at 0:17
  • 1
    The token data is probably in the req variable of the first snippet (which is actually a response). I don't see a need for a POST request here, GET should do. Usually data is in JSON form which can be decoded in a similar way as in the second snippet. Details how the token is saved in the JSON should be given by provider of the REST API. Commented Sep 8, 2018 at 0:28
  • @MichaelButscher - you are right thank you its working now , i have edited the first snippet with working code to get access token this will expire every 30 min how i can regenerate and use it through code Commented Sep 8, 2018 at 0:37

1 Answer 1

1

The token data is probably in the req variable of the first snippet (which is actually a response).

Usually the response data is in JSON format which can be decoded in a similar way as in the second snippet. In case of an XML format there are several parsers available, e.g. xml.etree.ElementTree from the Python standard library.

Details how the token is saved in the data and how to regenerate a token should be given by the provider of the REST API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.