Hey all,
I have a monday App to develop. Outside of moday, I am creating a small server to implement query and mutation with monday.com API for creating boards and items.
First of all I have to write the Python script to get the access token so that my app can be authorized for doing things.
Prefaces:
- I use Python.
- I built Flask server to have the redirect url for OAuth.
Now I am facing the messages:
{'error': 'invalid_grant', 'error_description': 'Invalid authorization code'}
To be more concrete, this is my Python scripts:
auth.py
import requests
def build_authorization_url(client_id, redirect_uri):
    authorization_url = 'https://auth.monday.com/oauth2/authorize'
    params = {
        'client_id': client_id,
        'redirect_uri': redirect_uri,
        'response_type': 'code',
    }
    return authorization_url + '?' + '&'.join([f'{k}={v}' for k, v in params.items()])
def fetch_access_token(client_id, client_secret, redirect_uri, code):
    token_url = 'https://auth.monday.com/oauth2/token'
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    data = {
        'grant_type': 'authorization_code',
        'client_id': client_id,
        'client_secret': client_secret,
        'redirect_uri': redirect_uri,
        'code': code,
    }
    response = requests.post(token_url, headers=headers, data=data)
    return response.json()
app.py This is my Flask script.
import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
from auth import build_authorization_url, fetch_access_token
client_id = 'XXX'
client_secret = 'XXX'
redirect_uri = 'http://127.0.0.1:5000/callback'
UPLOAD_FOLDER = 'C:\\dev\\monday.com_formUpdateApp\\temp_upload'
ALLOWED_EXTENSIONS = {'xls', 'csv'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
    return '.' in filename and \\
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('download_file', name=filename))
    return '''
    <!doctype html>
    <title>介接系統</title>
    <h1>請上傳您的Excel (.xls) 檔案</h1>
    <form method=post enctype=multipart/form-data>
      <input type=file name=file>
      <input type=submit value=Upload>
    </form>
    '''
@app.route('/authorize')
def authorize():
    authorization_url = build_authorization_url(client_id, redirect_uri)
    return redirect(authorization_url)
@app.route('/callback')
def callback():
    code = request.args.get('code')
    access_token = fetch_access_token(client_id, client_secret, redirect_uri, code)
    return access_token, code, "Access token obtained successfully!"
main.py
from auth import build_authorization_url, fetch_access_token
client_id = 'XXX'
client_secret = 'XXX'
redirect_uri = 'http://127.0.0.1:5000/callback'
authorization_url = build_authorization_url(client_id, redirect_uri)
code = 'the_code_returned_from_callback'
access_token = fetch_access_token(client_id, client_secret, redirect_uri, code)
print(access_token)
print(access_token) is what I want to check if the token is fetched successfully.
Is there any part that seems to be wrong?

