所以我正在为我在华盛顿的实习做一个编码项目。我的项目涉及使用 python 和 microsoft graph api 构建一个程序,检查在我公司获得的员工的电子邮件地址,以查看该电子邮件地址是否添加了另一种授权方法。如果检测到电子邮件地址的另一种授权方法,则可能意味着有人/不良行为者正在尝试访问信息。
我一直在引用 Jie Jenn 的视频开始使用 Microsoft Graph API 进行 Python 开发(设置和身份验证)。到目前为止,我可以从程序中获取设备代码和链接,但无法获取授权代码。除此之外,我还在 demo2.py 的第 31 行中遇到了一个回溯错误,在 main.py 的第 12 行和第 100 行中又遇到了 2 个回溯错误,并且 TypeError: 'dict' object is not callable in demo2.py 。 p>
这是我的代码。
谢谢你, 塞拉姆
错误:
''' 回溯(最近一次调用最后一次): 文件“C:\Users\S.Soundararajan\Documents\PE Project for Azure\demo2.py”,第 31 行,位于 webbrowser.open(流('verification_uri')) 类型错误:“dict”对象不可调用 '''
''' 回溯(最近一次调用最后一次): 文件“C:\Users\S.Soundararajan\Documents\PE Project for Azure\main.py”,第 100 行,位于 主要的() 文件“C:\Users\S.Soundararajan\Documents\PE Project for Azure\main.py”,第 12 行,在 main 中 图:图 = 图(azure_settings) 类型错误:Graph() 不带参数 Python 图形教程 '''
demo2.py:
`
#import account as account
import webbrowser
from xmlrpc.client import APPLICATION_ERROR
import requests
import msal
from msal import PublicClientApplication
CLIENT_ID = ''
CLIENT_SECRET = ''
authority_url = ''
base_url = 'https://graph.microsoft.com/v1.0/'
endpoint = base_url + 'me'
SCOPES = ['User.Read', 'Mail.Read', 'Mail.Send']
# Method 2. Login to acquire access_token
app = PublicClientApplication(
CLIENT_ID,
authority = authority_url
)
#accounts = app.get_accounts()
#if accounts:
#app.acquire_token_silent(scopes=SCOPES, account=account[0])
flow = app.initiate_device_flow(scopes=SCOPES)
print(flow)
print(flow['message'])
#app_code = flow['message']
webbrowser.open(flow('verification_uri'))
result = app.acquire_token_by_device_flow(flow)
access_token_id = result['access_token']
headers = {'Authorization': 'Bearer' + access_token_id}
response = requests.get(endpoint, headers=headers)
print(response)
print(response.json())
`
main.py:
import configparser
from graph import Graph
from msal import PublicClientApplication
def main():
print('Python Graph Tutorial\n')
# Load settings
config = configparser.ConfigParser()
config.read(['config.cfg', 'config.dev.cfg'])
azure_settings = config['azure']
graph: Graph = Graph(azure_settings)
greet_user(graph)
choice = -1
while choice != 0:
print('Please choose one of the following options:')
print('0. Exit')
print('1. Display access token')
print('2. List my inbox')
print('3. Send mail')
print('4. List users (requires app-only)')
print('5. Make a Graph call')
try:
choice = int(input())
except ValueError:
choice = -1
if choice == 0:
print('Goodbye...')
elif choice == 1:
display_access_token(graph)
elif choice == 2:
list_inbox(graph)
elif choice == 3:
send_mail(graph)
elif choice == 4:
list_users(graph)
elif choice == 5:
make_graph_call(graph)
else:
print('Invalid choice!\n')
def greet_user(graph: Graph):
user = graph.get_user()
print('Hello,', user['displayName'])
# For Work/school accounts, email is in mail property
# Personal accounts, email is in userPrincipalName
print('Email:', user['mail'] or user['userPrincipalName'], '\n')
def display_access_token(graph: Graph):
token = graph.get_user_token()
print('User token:', token, '\n')
return 1
def list_users(graph: Graph):
users_page = graph.get_users()
# Output each users's details
for user in users_page['value']:
print('User:', user['displayName'])
print(' ID:', user['id'])
print(' Email:', user['mail'])
# If @odata.nextLink is present
more_available = '@odata.nextLink' in users_page
print('\nMore users available?', more_available, '\n')
def list_inbox(graph: Graph):
message_page = graph.get_inbox()
# Output each message's details
for message in message_page['value']:
print('Message:', message['subject'])
print(' From:', message['from']['emailAddress']['name'])
print(' Status:', 'Read' if message['isRead'] else 'Unread')
print(' Received:', message['receivedDateTime'])
# If @odata.nextLink is present
more_available = '@odata.nextLink' in message_page
print('\nMore messages available?', more_available, '\n')
def send_mail(graph: Graph):
# Send mail to the signed-in user
# Get the user for their email address
user = graph.get_user()
user_email = user['mail'] or user['userPrincipalName']
graph.send_mail('Testing Microsoft Graph', 'Hello world!', user_email)
print('Mail sent.\n')
def make_graph_call(graph: Graph):
graph.make_graph_call()
# Run main
main()
`
最佳答案
问题1:
在代码库中,提到的 url 尚未声明,flow 无法找到要执行的地址。
通常,键值对应始终使用方括号来访问其中的值。线程中提到的代码之一需要使用 [] 来访问字典的元素。不是 ()
否则会得到 TypeError: The "dict"object is not callable 错误。
解决方案:
authority_url= 'https://docs.python.org/'
webbrowser.open_new(authority_url) // same window
webbrowser.open_new_tab(authority_url) // will open in new tab
问题 2:
引用官方tutorial .
关于python - 我无法从程序中获取授权码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75239614/