Download a File from Azure Devops Server Writes Wrong Data to The File

Download a file from azure devops server writes wrong data to the file

Finally I found solution to this issue.Here we are trying to use Personal access token(PAT).In order to use PAT for authentication we have to use authorization as "Basic" instead of "Bearer".More over instead of adding PAT alone to the Request header we have to use combination of username and PAT.Say base-64-encoded-string of username:PAT.

Required Code Change

var personalaccesstoken = "wwwwwwwwwwwwwwwwwwy47b7ugkz32bubi64bw7fqdyfpa";
var base64Creds = Convert.ToBase64String(Encoding.ASCII.GetBytes("SPabbbal@ABCTech.com:"+ personalaccesstoken));
request.Headers.Set(HttpRequestHeader.Authorization, "Basic " + base64Creds);

thank you for support.

How download file form azure DevOps sever to a specified path using API and c#

In C# this URL can be constructed like this (assume the variables are being passed in via a method call):

    var url = $" https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&api-version=5.1";

With the properly formatted URL we can then make a call to the API using the WebRequest class. For example:

var request = (HttpWebRequest)WebRequest.Create(address);
request.UserAgent = "VSTS-Get";
request.Headers.Set(HttpRequestHeader.Authorization, GetAuthenticationHeaderValue(authentication).ToString());
request.ContentType = "application/json";
request.Method = httpMethod;
response = (HttpWebResponse)request.GetResponse();

Using the HttpWebResponse object we can obtain a Stream object that we can then use to write out the API results to a local file. For example:

using (var responseStream = GetResponseStream(response))
{ var fileName = Path.Get Filename(fileToDownload ?? ""); using (var fileStream = File.Create(Path.Combine(destination, fileName))) { responseStream.CopyTo(fileStream); }
}

Please refer this link for more details: Get file API and sample blog

Update1

Thanks sagar for sharing, for more details, please refer to Download a file from azure devops server writes wrong data to the file

We should try to use Personal access token(PAT).In order to use PAT for authentication we have to use authorization as "Basic" instead of "Bearer".More over instead of adding PAT alone to the Request header we have to use combination of username and PAT.Say base-64-encoded-string of username:PAT.

Sample code:

var personalaccesstoken = "wwwwwwwwwwwwwwwwwwy47b7ugkz32bubi64bw7fqdyfpa";
var base64Creds = Convert.ToBase64String(Encoding.ASCII.GetBytes("SPabbbal@ABCTech.com:"+ personalaccesstoken));
request.Headers.Set(HttpRequestHeader.Authorization, "Basic " + base64Creds);

Find and download all instances of a file name in Azure Devops

Is there a way to download all files with a specific name from the
master branches of all the projects in an Azure DevOps installation?

This is not supported. Please check this document, we recommend different project(with one or more repos) for different products/sub-modules of big product. So in Azure Devops Service there's no such out-of-box feature to find/download files across projects.

A possible direction:

If those appsettings.json files are in root directory of your repos. You may save some time by using these two Rest APIs:

List all repos in current organization:

GET https://dev.azure.com/{OrganizationName}/_apis/git/repositories?api-version=5.1

Get File(Download):

GET https://dev.azure.com/{OrganizationName}/_apis/git/repositories/{Repositoryid}/items?scopePath=/appsettings.json&download=true&api-version=5.1

You can use PowerShell script to combine these two apis. The first one will list all Repositoryids in your organization, and the second one can download the appsettings.json from different repos via different Repositoryids. So the possible way could be run the first api once to get list of reposID(You can check this similar one) and then add a loop to get the files one by one.

Azure DevOps: How to download a file from git using REST API?

I have solved the problem, as both comments and previous answer are incorrect and will not lead you to the right answer for future visitors.

Following the example here as originally posted, it presumes that download=True: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/items/get?view=azure-devops-rest-6.1

However, you then need to query the file of interest and set this flag: &includeContent=true to actually get the file content from the devops GIT.

Like so:

https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items?scopePath=/MyWebSite/MyWebSite/Views/Home/_Home.cshtml&includeContent=true&api-version=6.1-preview.1

or you will send as many GET requests as you like and get nothing of value in return.



Related Topics



Leave a reply



Submit