Skip to content

Instantly share code, notes, and snippets.

@valeriocos
Last active May 4, 2022 07:03
Show Gist options
  • Save valeriocos/e16424bc7dc0f2d6dd8bb9295c6f9a4b to your computer and use it in GitHub Desktop.
Save valeriocos/e16424bc7dc0f2d6dd8bb9295c6f9a4b to your computer and use it in GitHub Desktop.
Get a bearer/OAuth2 token for Meetup application-only requests in Python3
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2019 Bitergia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Valerio Cosentino <valcos@bitergia.com>
#
import json
import requests
OAUTH_AUTHORIZE_URL = "https://secure.meetup.com/oauth2/authorize?client_id={}&redirect_uri={}&response_type=code"
OAUTH2_TOKEN_URL = 'https://secure.meetup.com/oauth2/access?client_id={}&client_secret={}&redirect_uri={}&code={}&grant_type=authorization_code'
def get_token_info(client_id, client_secret, redirect_uri, code):
access_uri = OAUTH2_TOKEN_URL.format(client_id, client_secret, redirect_uri.strip("\""), code)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
r = requests.post(access_uri, headers=headers)
r_json = r.json()
return r_json
def main():
"""This script assists the user to generate an OAuth2 token to access the
meetup API. It requires the package requests.
An example of execution is provided below:
Enter your consumer key: 5eeqs...
Redirect URI (between quotes): "https://example.com/test"
https://secure.meetup.com/oauth2/authorize?client_id=5eeqs4f5h...&redirect_uri=https://example.com/test&response_type=code
Access the URL below, authorize the application, and get the code that appears on the URL
Enter the code: d33fd...
Enter your consumer secret: 1o39i...
***** token *****
{
"access_token": "67bd7...",
"expires_in": 3600,
"refresh_token": "c6e13...",
"token_type": "bearer"
}
"""
consumer_key = input('Enter your consumer key: ')
redirect_uri = input('Redirect URI (between quotes): ')
url = OAUTH_AUTHORIZE_URL.format(consumer_key, redirect_uri.strip("\""))
print(url)
print("Access the URL below, authorize the application, and get the code that appears on the URL")
code = input('Enter the code: ')
consumer_secret = input('Enter your consumer secret: ')
info = get_token_info(consumer_key, consumer_secret, redirect_uri, code)
print("***** token *****")
print(json.dumps(info, sort_keys=True, indent=4))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment