如何编写 Python 脚本以对 Azure DevOps REST API 进行身份验证并获取访问令牌? [英] How to write a python script to authenticate to Azure DevOps REST API and get the access token?

查看:23
本文介绍了如何编写 Python 脚本以对 Azure DevOps REST API 进行身份验证并获取访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 python 脚本中对 Azure DevOps REST API 进行身份验证?我发现有两种方法:

How can I authenticate to Azure DevOps REST API in a python script? I found that there are 2 methods :

  • 使用个人访问令牌 (PAT)
  • 使用 OAuth 2.0

我正在使用第二种方法.遵循本文档中的步骤:https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops

I am using the second method. Followed the steps in this documentation: https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops

我编写了这个函数来使用 OAuth 2.0 对 azure DevOps 进行身份验证:

I wrote this function to autherize to azure DevOps using OAuth 2.0:

def get_authenticated():

    client_id = < my client ID as a string >
    state = "user1"
    scope = "vso.graph_manage%20vso.identity_manage%20vso.profile_write%20vso.project_manage%20vso.tokenadministration%20vso.tokens"
    callback_URL = < Callback URL to my azure devops account >

    # Azure DevOps Services authorization endpoint
    Auth_URL = "https://app.vssps.visualstudio.com/oauth2/authorize?client_id=" + client_id + "&response_type=Assertion&state=" + state + "&scope=" + scope + "&redirect_uri=" + callback_URL
    headers = {'Accept': 'application/json;api-version=1.0'}

    print(Auth_URL)

    response = requests.get(Auth_URL,headers = headers)
    print(response)
    print(response.status_code)
    print(response.headers['content-type'])

    response.raise_for_status() 

但是当调用这个函数时,我得到的输出是:

But when calling this function, output I am getting is:

<Response [203]>
203
text/html; charset=utf-8

身份验证 URL 是正确的,因为当我尝试在浏览器中访问相同的 URL 时,它成功重定向到表单以输入 Azure 用户凭据.

The auth URL is correct because when I tried to access the same URL in a browser it successfully redirects to a form to enter azure user credentials.

脚本的预期行为是,当请求 auth_url 时,Azure DevOps Services 应要求用户授权.我认为这应该通过在终端/浏览器中提示输入用户名和密码来完成.

The expected behavior of the script is, when the auth_url is requested, Azure DevOps Services should ask the user to authorize. I think that should be done by prompting for username&password in terminal/via a browser.

我对 Python 脚本和 REST API 完全陌生.有人可以通过指出我的代码中的错误或指出一些示例来帮助我吗?

I am totally new to python scripting and REST APIs. Can someone help me by pointing out the faults in my code or pointing to some samples?

推荐答案

http 错误 203 表示返回的元信息不是来自具有对象副本的服务器的最终对象集,而是来自私有覆盖网.在您的代码中,您添加了 headers = {'Accept': 'application/json;api-version=1.0'},但实际上内容类型应该是 application/x-www-表单 urlencoded.

The http error 203 indicates that the returned metainformation is not a definitive set of the object from a server with a copy of the object, but is from a private overlaid web. In your code,you added headers = {'Accept': 'application/json;api-version=1.0'}, but in fact the content type should be application/x-www-form-urlencoded.

您可以使用一些适用于 Python 的 OAuth2 库向 Azure DevOps REST API 进行身份验证,例如 OAuthLib.它包括几个样本.

You can use some OAuth2 library for python to authenticate to Azure DevOps REST API, such as OAuthLib. It includes sevelral samples.

另外,您可以参考以下主题,希望对您有所帮助.

Also, you can refer to following topic, hope it is helpful for you.

requests_oauth2 使用教程

这篇关于如何编写 Python 脚本以对 Azure DevOps REST API 进行身份验证并获取访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆