-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_slack_permissions.py
executable file
·133 lines (107 loc) · 4.46 KB
/
test_slack_permissions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
"""
Test script to verify Slack API token permissions.
This script tests the required scopes for the Slack integration:
- users:read (for member count)
- channels:read (for channel list)
"""
import os
import sys
import requests
import time
from dotenv import load_dotenv
# Load environment variables from .env file if it exists
load_dotenv()
# Get the Slack API token
SLACK_API_TOKEN = os.getenv("SLACK_API_TOKEN")
if not SLACK_API_TOKEN:
print("Error: SLACK_API_TOKEN environment variable is not set.")
print("Please set it in your environment or .env file.")
sys.exit(1)
# Print token prefix for verification (only first 10 chars for security)
print(f"Token prefix: {SLACK_API_TOKEN[:10]}...")
# Set up headers for API requests - REMOVED Content-Type header
headers = {
"Authorization": f"Bearer {SLACK_API_TOKEN}"
}
def test_scope(scope_name, api_url, params=None):
"""Test a specific Slack API scope."""
print(f"\nTesting {scope_name} scope...")
try:
response = requests.get(api_url, headers=headers, params=params)
data = response.json()
if response.status_code != 200:
print(f"HTTP Error: {response.status_code}")
print(f"Response: {response.text}")
return False
if not data.get("ok", False):
error = data.get("error", "unknown error")
print(f"Slack API Error: {error}")
if error == "missing_scope":
print(f"Your token is missing the required {scope_name} scope.")
elif error == "invalid_auth":
print("Your token is invalid or has been revoked.")
elif error == "not_authed":
print("No authentication token provided or token is invalid.")
elif error == "account_inactive":
print("Authentication token is for a deleted user or workspace.")
elif error == "token_revoked":
print("Authentication token has been revoked.")
elif error == "rate_limited":
retry_after = response.headers.get("Retry-After", "unknown")
print(f"Rate limited. Retry after: {retry_after} seconds")
return False
print(f"✅ {scope_name} scope is working correctly!")
return True
except Exception as e:
print(f"Error testing {scope_name} scope: {e}")
return False
# Test users:read scope
users_success = test_scope(
"users:read",
"https://slack.com/api/users.list",
{"limit": 1} # Only request 1 user to minimize data transfer
)
# Add a small delay to avoid rate limiting
time.sleep(1)
# Test channels:read scope - Only public channels
channels_success = test_scope(
"channels:read",
"https://slack.com/api/conversations.list?types=public_channel"
)
# If channels:read failed, we can't test history scopes properly
if not channels_success:
print("\nCannot test history scopes because channels:read failed.")
sys.exit(1)
# Add a small delay to avoid rate limiting
time.sleep(1)
# Get a channel ID for testing history scopes - Only public channels
channel_response = requests.get(
"https://slack.com/api/conversations.list?types=public_channel",
headers=headers
)
channel_data = channel_response.json()
if not channel_data.get("ok") or not channel_data.get("channels"):
print("\nCould not find any channels to test history scopes.")
sys.exit(1)
channel_id = channel_data["channels"][0]["id"]
channel_name = channel_data["channels"][0]["name"]
print(f"\nUsing channel #{channel_name} (ID: {channel_id}) for history tests")
# Add a small delay to avoid rate limiting
time.sleep(1)
# We're only testing users:read and channels:read scopes
# No need to test channels:history or groups:history
print("\nSkipping channels:history and groups:history tests as they are not required")
channels_history_success = None
groups_history_success = None
# Summary
print("\n=== Summary ===")
print(f"users:read: {'✅ Working' if users_success else '❌ Failed'}")
print(f"channels:read: {'✅ Working' if channels_success else '❌ Failed'}")
print("channels:history: Skipped (not required)")
print("groups:history: Skipped (not required)")
if users_success and channels_success:
print("\nAll required scopes are working correctly! 🎉")
print("The Slack integration only needs users:read and channels:read scopes.")
else:
print("\nSome required scopes are not working. Please check the errors above.")