You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

34 lines
899 B

# -*- coding: utf-8 -*-
import requests
from datetime import datetime, timedelta
def get_token(code, client_id, client_secret):
"""
Usage:
$ python manage.py shell
>>> from yandex_webmaster import get_token
>>>> get_token(code, client_id, client_secret)
"""
post_data = {
'code': str(code),
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'authorization_code',
}
token_url = 'https://oauth.yandex.ru/token'
t = datetime.now()
req = requests.post(token_url, data=post_data)
if req.status_code == 200:
req_json = req.json()
print 'token:', req_json['access_token']
print 'expires:', t + timedelta(seconds=req_json['expires_in'])
print 'now:', t
else:
print u'%s %s. Error: %s' % (req.status_code, req.reason, req.text)