Created
December 24, 2015 12:24
-
-
Save linuxoid69/92c830ad5feb2c7441d8 to your computer and use it in GitHub Desktop.
Получение токена для яндекс диска
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from bottle import route, run, request | |
import httplib | |
import urllib | |
import json | |
#Идентификатор приложения | |
client_id = '' | |
#Пароль приложения | |
client_secret = '' | |
@route('/') | |
def index(): | |
#Если скрипт был вызван с указанием параметра "code" в URL, | |
#то выполняется запрос на получение токена | |
if request.query.get('code'): | |
#Формирование параметров (тела) POST-запроса с указанием кода подтверждения | |
query = { | |
'grant_type': 'authorization_code', | |
'code': request.query.get('code'), | |
'client_id': client_id, | |
'client_secret': client_secret, | |
} | |
query = urllib.urlencode(query) | |
#Формирование заголовков POST-запроса | |
header = { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
#Выполнение POST-запроса и вывод результата | |
connection = httplib.HTTPSConnection('oauth.yandex.ru') | |
connection.request('POST', '/token', query, header) | |
response = connection.getresponse() | |
result = response.read() | |
connection.close() | |
#Токен необходимо сохранить для использования в запросах к API Директа | |
return json.loads(result)['access_token'] | |
#Запускаем веб-сервер | |
run(host='localhost', port=8080, quiet=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment