Skip to content

Instantly share code, notes, and snippets.

@enzzc
Created October 6, 2018 14:15
Show Gist options
  • Save enzzc/87f1c89360134972ab43b49a6262e14e to your computer and use it in GitHub Desktop.
Save enzzc/87f1c89360134972ab43b49a6262e14e to your computer and use it in GitHub Desktop.
from bottle import route, run, template, request
import urllib.parse
import requests
CLIENT_ID = 'client_id'
CLIENT_SECRET = 'client_secret'
REDIRECT_URI = 'http://localhost:5555/oauth_line'
@route('/')
def index():
line_url = 'https://notify-bot.line.me/oauth/authorize'
params = {
'client_id': CLIENT_ID,
'response_type': 'code',
'state': 42,
'scope': 'notify',
'redirect_uri': REDIRECT_URI
}
url = line_url + '?' + urllib.parse.urlencode(params)
return template('<a href="%s">Click here to login with LINE</a>' % url)
@route('/oauth_line')
def oauth_line():
code = request.query['code']
r = requests.post(
'https://notify-bot.line.me/oauth/token',
data={
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'code': code,
'grant_type': 'authorization_code',
'redirect_uri': REDIRECT_URI
}
)
res = r.json()
access_token = res['access_token'] # Now, save this token!
r = requests.post(
'https://notify-api.line.me/api/notify',
data={
'message': 'Welcome!'
},
headers={
'Authorization': 'Bearer ' + access_token
}
)
run(host='localhost', port=5555)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment