简体   繁体   English

如何通过 Python 连接到 Bitbucket?

[英]How to connect to Bitbucket via Python?

I am facing difficulties connecting to the Bitbucket repo from python.我在从 python 连接到 Bitbucket 存储库时遇到困难。 I have come across few python modules which let us connect to the Bitbucket repository from python.我遇到了几个 python 模块,它们让我们从 python 连接到 Bitbucket 存储库。 I have tried 'Bitbucket-python' which enables me to connect to the Bitbucket repo but still i am not able to list out the repositories by using the same module.我尝试了“Bitbucket-python”,它使我能够连接到 Bitbucket 存储库,但我仍然无法使用相同的模块列出存储库。

Can you please suggest me any python module which i can use for the purpose?你能建议我使用任何 python 模块吗?

I am stuck.我被困住了。 Any help would be appreciated.任何帮助,将不胜感激。

Thanks, Bikram谢谢,比克拉姆

You can use stashy module.您可以使用stashy模块。

For example:例如:

import stashy

bitbucket = stashy.connect("host", "username", "password")
projects = bitbucket.projects.list()
repos = bitbucket.repos.list()

for project in projects:
    for repo in bitbucket.projects["%s" % (project["key"])].repos.list():
        print(repo["name"])
        print(repo["project"]['key'])

I would recommend using python's requests module and this url https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-group-repositories as reference我建议使用 python 的 requests 模块和这个 url https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-group-repositories作为参考

To see if you have a connection maybe do something like this: The {workspace} is your workspaceid and the api key I generated under the security tab in you bitbucket account.要查看您是否有连接,可以执行以下操作:{workspace} 是您的工作空间 ID,而我在 bitbucket 帐户的安全选项卡下生成的 api 密钥。 Good luck!祝你好运!

 import requests import json headers = { '{API key}': '{API secret}' 'Content-Type': 'application/json' } #Get repo list url = "https://api.bitbucket.org/2.0/repositories/{workspace}" response = requests.request("GET", url, headers=headers) print(response) # 200 reponse means there is a connection #or if you have repos try this to print them print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM