Gábor Domonkos
Gábor Domonkos

Reputation: 1111

Bitbucket authentication to use LibGit2Sharp

I used the CSharp.Bitbucket library to authenticate users with Bitbucket (https://github.com/scottksmith95/CSharp.Bitbucket). The authentication works fine, I get the token value and token secret values.

I have alredy written a logic - with the help of LibGit2Sharp (https://github.com/libgit2/libgit2sharp) - to clone/pull/push the content of the users repo. It works fine if the user authenticates with GitHub. In that case I have to provide the value of the access token for username and an empty string for password.

 LibGit2Sharp.Credentials credentials = new UsernamePasswordCredentials()
        {
            Username = [GITHUB ACCESS TOKEN], 
            Password =""
        };

But what about Bitbucket? Can I use LibGit2Sharp in this way? I couldn't find any documentation about this issue. I tried to use the token value and the token secret also for username, but it fails.

enter image description here

I would like to use Bitbucket with Git and Mercurial too. Is there a little trick which I couldn't realize this far?

Thank you for your help!

Upvotes: 3

Views: 3970

Answers (3)

Kebechet
Kebechet

Reputation: 2387

[2022] You can use LibGit2Sharp even for Bitbucket but you don't need any tokens. There is a little bit different flow by using app password. (This works even with 2FA enabled.)

  1. In the bitbucket go to:

    • https://bitbucket.org/account/settings/app-passwords/
    • or manually navigate: Click your picture -> Personal settings -> App passwords
  2. Click Create app password

    • Name it after the service in which you will use it/or for what purpose
    • Add needed permissions
    • hit create
  3. Find your bitbucket name:

    • navigate to https://bitbucket.org/account/settings/
    • or Click your picture -> Personal settings -> Account settings
    • and in sub-category Bitbucket profile settings is your Username
  4. apply these credentials to your code. E.g.

    var url = "https://bitbucket.org/<user_or_org_name>/<repository_name>.git";
    var Username = "<your_user_name>";
    var Password = "<your_app_password>";
    var localPathForRepository = @"D:\temp";
    
    var creds = new UsernamePasswordCredentials()
    {
        Username = Username,
        Password = Password
    };
    
    CredentialsHandler credHandler = (_url, _user, _cred) => creds;
    
    bool dirExists = Directory.Exists(localPathForRepository);
    if (dirExists)
    {
        Directory.Delete(localPathForRepository, true);
    }
    Directory.CreateDirectory(localPathForRepository);
    
    var cloneOptions = new CloneOptions { BranchName = "master", Checkout = true, CredentialsProvider = credHandler };
    var cloneResult = Repository.Clone(url, localPathForRepository, cloneOptions);
    
  5. DONE

Sources:

https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/

https://id.atlassian.com/manage-profile/security/api-tokens

https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/#Use-an-API-token

Upvotes: 2

Rafael Juan
Rafael Juan

Reputation: 41

This question is old but I will answer for those how come across whith this problem. First, what you have to do is get an acces token. Its well explaned here.

Once you have the token, you can authenticate like this

   CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
        {
            Username = x-token-auth:[ACCES-TOKEN],
            Password = ""
        } 

Hope it helps someone.

Upvotes: 4

nulltoken
nulltoken

Reputation: 67639

This way of passing the token as the username with a blank password is specific to GitHub AFAIK (see this help article). The article also states that passing x-oauth-basic works.

The BitBucket issue #7735 seems to state that passing the token as the username and x-oauth-basic would also work.

Upvotes: 3

Related Questions