我试图使用OAuth2访问Azure DevopsAPI,查询工作项。但我是,无法获得访问令牌e。
我用的是Python和烧瓶。我的方法是基于这些资源:
相关图书馆:
from requests_oauthlib import OAuth2Session
from flask import Flask, request, redirect, session, url_for参数:
client_id = "..."
client_secret = "..."
authorization_base_url = "https://app.vssps.visualstudio.com/oauth2/authorize"
token_url = "https://app.vssps.visualstudio.com/oauth2/token"
callback_url = "..."步骤1:用户授权.(工作正常)
@app.route("/")
def demo():
azure = OAuth2Session(client_id)
authorization_url, state = azure.authorization_url(authorization_base_url)
session['oauth_state'] = state
authorization_url += "&scope=" + authorized_scopes + "&redirect_uri=" + callback_url
print(authorization_url)
return redirect(authorization_url)步骤2:检索访问令牌(生成错误)
@app.route("/callback", methods=["GET"])
def callback():
fetch_body = "client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer" \
"&client_assertion=" + client_secret + \
"&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer" \
"&assertion=" + request.args["code"] + \
"&redirect_uri=" + callback_url
azure = OAuth2Session(client_id, state=session['oauth_state'])
token = azure.fetch_token(token_url=token_url, client_secret=client_secret,
body=fetch_body,
authorization_response=request.url)
azure.request()
session['oauth_token'] = token
return redirect(url_for('.profile'))应用程序注册和adhoc-SSL认证工作正常(使用它只是临时的)。
当我在邮递员中使用client_assertion时,我从Azure那里得到了正确的答复:

但是,当我执行代码时,会引发以下错误:
oauthlib.oauth2.rfc6749.errors.MissingTokenError: (missing_token) Missing access token parameter.这只会让我知道没有收到任何象征性的东西。
生成的请求体中有一个问题,其中添加了两次grant_type:
grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer
grant_type=authorization_code第一个值由Azure期望,第二个值由库自动生成。
现在,当我在grant_type调用中指定fetch_token时,如下所示:
token = azure.fetch_token(token_url=token_url, client_secret=client_secret,
body=fetch_body, grant_type="urn:ietf:params:oauth:grant-type:jwt-bearer",
authorization_response=request.url)我知道这个错误
TypeError: prepare_token_request() got multiple values for argument 'grant_type'对Azure的实际请求甚至都没有发送。
我在oauth2_session.py使用的oauth2_session.py中看到,grant_type =‘grant_type_code’被设置为固定的,所以我猜这个库通常与Azure不兼容。
是这样吗?如果是这样的话,用Python连接Azure-OAuth最简单的方式是什么?
我将非常感谢任何建议,并帮助我指出正确的方向。
发布于 2022-09-23 18:23:40
我刚刚找到了解决问题的azure.devops库。
资源来源
from azure.devops.connection import Connection
from azure.devops.v5_1.work_item_tracking import Wiql
from msrest.authentication import BasicAuthentication
import pprint
# Fill in with your personal access token and org URL
personal_access_token = '... PAT'
organization_url = 'https://dev.azure.com/....'
# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get a client (the "core" client provides access to projects, teams, etc)
core_client = connection.clients.get_core_client()
wit_client = connection.clients.get_work_item_tracking_client()
query = "SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State]," \
"[System.Tags] FROM workitems WHERE [System.TeamProject] = 'Test'"
wiql = Wiql(query=query)
query_results = wit_client.query_by_wiql(wiql).work_items
for item in query_results:
work_item = wit_client.get_work_item(item.id)
pprint.pprint(work_item.fields['System.Title'])https://stackoverflow.com/questions/73765584
复制相似问题