My flask application is giving error of scope when using tokens to retrieve the zoom team-chats

  Kiến thức lập trình

I have created a flask app that uses slack api to login and retrieve credentials but when i use the credentials to retrieve the zoom team chat detail it give me scope error, Although my zoom app have all the required scopes annd permission attached. Please review my attached code and help me fix this error

from flask import Flask, redirect, request, session, jsonify
import requests
import base64
import logging
import os

# Setup logging
logging.basicConfig(level=logging.DEBUG)

app = Flask(__name__)
app.secret_key = 'YOUR_SECRET_KEY'

# Zoom Credentials
CLIENT_ID = 'obUXAO2rSvm3yKtbHvNumg'
CLIENT_SECRET = '58DwrJ4xA6rMTFRMIZ5QzfrXj0qkPd8A'
REDIRECT_URI = 'http://localhost:8080/oauth/callback'

def save_token(token):
    with open('token.txt', 'w') as file:
        file.write(token)

def get_saved_token():
    if os.path.exists('token.txt'):
        with open('token.txt', 'r') as file:
            return file.read()
    return None

@app.route('/')
def home():
    return '<a href="/login">Login with Zoom</a>'

@app.route('/login')
def login():
    auth_url = f"https://zoom.us/oauth/authorize?client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}"
    return redirect(auth_url)

@app.route('/oauth/callback')
def callback():
    code = request.args.get('code')
    if not code:
        logging.error("No code received from Zoom authorization")
        return "Error: No code received", 400

    headers = {'Authorization': 'Basic ' + base64.b64encode(f"{CLIENT_ID}:{CLIENT_SECRET}".encode()).decode()}
    payload = {
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': REDIRECT_URI
    }
    token_response = requests.post('https://zoom.us/oauth/token', headers=headers, params=payload)

    if token_response.status_code != 200:
        logging.error(f"Failed to get token: {token_response.text}")
        return f"Error getting token: {token_response.text}", 500

    access_token = token_response.json().get('access_token')
    session['access_token'] = access_token
    save_token(access_token)
    return redirect('/all_chats')

@app.route('/all_chats')
def all_chats():
    access_token = session.get('access_token') or get_saved_token()
    if not access_token:
        return "Authentication required", 401

    headers = {'Authorization': f'Bearer {access_token}'}
    channels_response = requests.get('https://api.zoom.us/v2/chat/channels', headers=headers)
    if channels_response.status_code != 200:
        logging.error(f"Failed to retrieve channels: {channels_response.text}")
        return jsonify({
            "error": "Failed to retrieve channels",
            "message": channels_response.text
        }), 500

    channels = channels_response.json()
    all_messages = []

    for channel in channels.get('channels', []):
        channel_id = channel['id']
        messages_response = requests.get(f'https://api.zoom.us/v2/chat/channels/{channel_id}/messages', headers=headers)
        if messages_response.status_code == 200:
            messages = messages_response.json().get('messages', [])
            all_messages.append({channel['name']: messages})
        else:
            logging.error(f"Failed to retrieve messages for channel {channel['name']}: {messages_response.text}")

    return jsonify(all_messages)

if __name__ == '__main__':
    app.run('localhost', 8080, debug=True)

LEAVE A COMMENT