【问题标题】:Unable to fetch OAuth2.0 Token from Azure-DevOps , using Python无法使用 Python 从 Azure-DevOps 获取 OAuth2.0 令牌
【发布时间】:2022-09-24 02:23:40
【问题描述】:

我正在尝试使用 OAuth2 访问 Azure DevopsAPI,以查询工作项。 但我是无法获取访问令牌e.

我正在使用 Python 和 Flask。我的方法基于以下资源:

相关库:

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\'))

application-registration 和 adhoc-SSL-certification 工作正常(只是临时使用)。

当我在 Postman 中使用 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 需要第一个值,但第二个值由库自动生成。

现在,当我在 fetch_token 调用中指定 grant_type 时,如下所示:

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 使用的 web_application.py 中看到,grant_type =\'authorization_code\' 设置为固定的,所以我猜这个库通常与 Azure 不兼容。

是这样吗? 如果是这样,使用 Python(Flask)连接到 Azure-OAuth 的最简单方法是什么?

我将非常感谢任何可以为我指明正确方向的建议和帮助。

    标签: python flask azure-devops oauth-2.0


    【解决方案1】:

    我刚刚找到了解决我的问题的 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'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-27
      • 2013-09-02
      • 2019-09-20
      • 2022-01-22
      • 2020-10-16
      • 2019-10-14
      • 1970-01-01
      • 2019-02-15
      相关资源
      最近更新 更多