使用访问令牌下载bitBucket私有存储库的powershell脚本 [英] powershell script download of bitBucket private repository using access token

查看:74
本文介绍了使用访问令牌下载bitBucket私有存储库的powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过首先按如下方式从bitbucket中获取代码来使构建过程自动化.

I am trying to automate a build process by first getting the code from bitbucket as follows.

$output = "E:\FileName.xyz"
$url = 'https://bitbucket.org/WhatEver/WhatEverBranchName/get/master.zip'
$wc = New-Object -TypeName System.Net.WebClient
$wc.Headers.Add('Authorization','token oinksdilikncl--MyAccessToken--ioiwcnoisif89kfg9')
$wc.DownloadFile($url, $output)

当我执行此操作时,我在FileName.xyz处收到的文件是一个html文件,即使我提供了访问令牌,该文件也将我重定向到bitbucket的登录页面,从本质上讲是要求信誉的.

When I execute this, The file i receive at FileName.xyz is a html file that redirects me to the loginpage of the bitbucket, essentially its asking for creds, even though I suppiled access token.

我在哪里错了?还有其他方法可以说是Invoke-Webrequest吗?还是有人请我指导一个代码示例?

Where am I wrong? Are there other ways to do this say, Invoke-Webrequest? Or someone kindly direct me to a code sample please?

推荐答案

我在Powershell方面的经验绝对为零,但是我尝试在node中执行类似的任务,这是我的发现.

I have absolutely zero experience in powershell, but I tried to do the similar task in node and here are my findings.

  1. 首先,您在bitbucket帐户设置的访问管理部分中创建"Oauth".这为您提供了一个密钥"和一个秘密".

  1. First you create an "Oauth" in access management section of your bitbucket account setting. This gives you a "Key" and a "Secret".

现在,使用这些密钥和秘密",您要求Bitbucket提供令牌.就我而言,我向 https://bitbucket.org/site/oauth2/access_token 发出了http请求.在您的情况下,您应该使用等效的CURL(也许是Invoke-RestMethod?).CURL命令是这样的:

Now using these Key and Secret you ask Bitbucket for a token. In my case I made a http request to https://bitbucket.org/site/oauth2/access_token. In your case you should use an equivalent of CURL (Invoke-RestMethod maybe?). the CURL command is like this:

$ curl -X POST -u "yourKeyHere:yourSecretHere" https://bitbucket.org/site/oauth2/access_token -d grant_type=client_credentials

我的http请求是这样的(在节点中使用超级代理),我的 Content-Type 设置为 application/x-www-form-urlencoded :

my http request was like this (using superagent in node) with my Content-Type set to application/x-www-form-urlencoded:

request.post("https://yourKeyHere:yourSecretHere@bitbucket.org/site/oauth2/access_token").send('grant_type=client_credentials');

  • 现在您有了令牌,您的程序或命令就可以使用它克隆一个私有仓库.但是,您的仓库的网址应该是这样的(将令牌括在括号中):

  • Now that you have the token, your program or your command can clone a private repo with it. But the url to your repo should be like this (keep the bracket around token):

    https://x-token-auth:{tokenHere}@bitbucket.org/youRepoOwnerHere/RepoNameHere.git
    

  • 现在,您已经在计算机上拥有了完整的代码库.但是您只需要一个文件,而不是整个仓库,我将您引荐给这个从存储库中检索单个文件,但请记住,将上面的repo url与令牌一起使用,而不是常规的repo url.

  • Now you have the whole codebase on your machine. But you want a single file rather than the whole repo which I refer you to this Retrieve a single file from a repository but remember to use above repo url with the token instead of a normal repo url.

    这篇关于使用访问令牌下载bitBucket私有存储库的powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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