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

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

问题描述

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

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/zh-CN/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-form-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.

您可以使用某些OAuth2库(用于python)对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天全站免登陆