使用Python 3.6使用个人访问令牌对VisualStudioOnline REST API进行身份验证 [英] Authenticating to VisualStudioOnline REST API with Personal Access Token using Python 3.6

查看:115
本文介绍了使用Python 3.6使用个人访问令牌对VisualStudioOnline REST API进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过python 3.6使用VisualStudioOnline REST API. (大量使用python 2.x的示例.)

I am trying to use the VisualStudioOnline REST API using python 3.6. (Plenty of examples using python 2.x.)

python脚本响应是通用的html登录页面.

The python script response is the generic html login page.

我已经使用REST Console Chrome插件测试了此脚本生成的url,并且使用我的个人访问令牌可以正常工作.

I have tested the url generated by this script using REST Console Chrome plug-in and it worked fine using my personal access token.

import json
import base64
import urllib.request

personal_access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

headers = {}
headers['Content-type'] = "application/json"
headers['Authorization'] = b'Basic ' + 
base64.b64encode(personal_access_token.encode('utf-8'))

instance = "mycompany.visualstudio.com"
project = "MyProject"
repository ="MyRepository"
pullrequest = "3468"
api_version = "3.0"

repositories_url = ("https://%s/DefaultCollection/%s/_apis/git/repositories?
api-version=%s" % (instance, project, api_version))
print(repositories_url)

request = urllib.request.Request(repositories_url, headers=headers)
opener = urllib.request.build_opener()
response = opener.open(request)
print(response.read())

Powershell示例

Powershell Example

如何我如何使用个人访问令牌向Visual Studio Team Services进行身份验证?

C#和curl示例

https://www.visualstudio.com/en-us/docs/integrate/get-started/authentication/pats

推荐答案

根据我通过其他类似机制执行此操作的经验,在进行base64编码之前,必须在PAT上包含前导冒号.

In my experience with doing this via other similar mechanisms, you have to include a leading colon on the PAT, before base64 encoding.

这篇关于使用Python 3.6使用个人访问令牌对VisualStudioOnline REST API进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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