109

I have created an app password as explained here

But now, how do I access the repository using this app password?
What will be the url?
Can someone direct me to a page showing an example please?

The below is a code for github. How do I do it for bitbucket?

var githubToken = "[token]";
var url = "https://github.com/[username]/[repository]/archive/[sha1|tag].zip";
var path = @"[local path]";


using (var client = new System.Net.Http.HttpClient())
{
    var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", githubToken);
    credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
    var contents = client.GetByteArrayAsync(url).Result;
    System.IO.File.WriteAllBytes(path, contents);
}

Update

Go to Personal Settings and then App Passwords as shown below.

Create Bitbucket App Password

VivekDev
  • 20,868
  • 27
  • 132
  • 202

7 Answers7

132

Since the question was "how do I access the repository" - maybe this is useful for somebody:
You can also use a Bitbucket "App Password" to use Git over HTTPS.

git clone https://USERNAME:APP_PASSWORD@bitbucket.org/OWNER/REPO.git

(Or, if you want to avoid storing the password in plaintext:
Omit :APP_PASSWORD in the URL above, and just provide the App Password when prompted by Git.)

This may be useful for CI (although Git over SSH might be better).

mh8020
  • 1,784
  • 1
  • 11
  • 14
  • 78
    IF you already cloned repository, use this `git remote set-url origin https://USERNAME:APP_PASSWORD@bitbucket.org/OWNER/REPO.git` – junho Mar 17 '21 at 02:09
  • 2
    ok. It really isn't obvious. You need to set your Remote URL/Path in SourceTree Settings as follows https://:@bitbucket.org/path-to-source-master/src/master/ The user name is the main account user name, which is found under Bitbucket profile settings (Username) under Account Settings at https://bitbucket.org/account/settings/ The app password is set in https://bitbucket.org/account/settings/app-passwords/ – David Lipschitz Mar 14 '22 at 12:51
  • 1
    Seems like this method is exposing the password in clear text.. – QT-1 May 07 '22 at 00:12
110

You can perform this request as follows:

curl -u "walery2iq:<appPassword>" "https://api.bitbucket.org/2.0/repositories/[yourRepo]"

Important thing here - use your username, not e-mail address. Important because for login on bitbucket itself you are using e-mail and not username :D

You can find your username here:

Settings -> Account settings -> Username

See picture.

Use this username - not your mail address

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
75

You need to create your "app password" on bitbucket console. Under your Avatar ->Personal Settings ->Access Management ->App Password. Create app password and note it down.

Once done, open your git bash and set the remote origin using -

git remote set-url origin https://<Your_Account_Name>:<App_Password>@bitbucket.org/<Your_Account_Name>/<Repo_Name>.git
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Kunal Keshari
  • 751
  • 3
  • 2
  • 1
    In the last part, you are not required to give Account_name again instead need to provide a path for the repo. bitbucket.org/OWNER/REPO.git – mujaffars Mar 10 '22 at 05:10
  • 1
    this is the one that works for me. but maybe just a little correction: it should be https://:@bitbucket.org/... – dapidmini Mar 14 '22 at 10:49
20

If you have a 2-step verification I was able to clone but not to make a push. This worked for me using git instead of curl:

git push https://<username>:<GENERATED-APP-PASS>@bitbucket.org/<username>/<repo>.git

Generate an app password at https://bitbucket.org/account/admin/app-passwords

Victor
  • 504
  • 3
  • 9
8
git remote set-url origin https://[app-label]:[app-password]@bitbucket.org/[your-repo].git
ShSehati
  • 508
  • 5
  • 7
  • 1
    Did not work. Says invalid credentials after trying to push. Kunal Keshari solution worked. You need Account_Name instead of app-label. And you need to repeat it after bitbucket.org/ – Ville Miekk-oja Mar 05 '22 at 10:36
1

Git uses libcurl under the hood so one might keep password in ~/.netrc file:

machine bitbucket.org login USER password PAZZ

and avoid leaking password to shell history:

git clone https://$USER@bitbucket.org/$WORKSPACE/$REPO.git
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
1

This worked for me, check use credential helper from git settings. That is, preferences-> version control-> To git -> use credential helper

victor
  • 11
  • 1