Cannot clone git from Azure DevOps using PAT

2019-03-30 02:11发布

问题:

I cannot clone a simple repository from Azure DevOps. OS: Ubuntu 18.10

I do this:

  • Got to Azure DevOps
  • Click on the top right corner on my user name
  • Go to the security tab
  • Create a PAT with all the scope (to be sure there is no scope problem)
  • Copy the PAT to the clipboard
  • Paste the PAT somewhere else to confirm the PAT has been copied correctly (I know... not safe but that is out of the scope of this question)
  • Go to my console
  • Issue: git clone https://myorganization.visualstudio.com/myproject/_git/myrepo/
  • I enter the user name and the password (the PAT)

I keep getting this message from Git: fatal: Authentication failed for 'https://myorganization.visualstudio.com/myproject/_git/myrepo/'

I was reading the solutions of other people but none worked for me. I also tried this:

  • Pass the token in the form https://usename:token@myorganization.visualstudio.com/myproject/_git/myrepo/ I am really surprised people say this worked... for my git complains because believes the : means the port.
  • Tried to activate/deactivate credential manager of git * Tried on Windows and Ubuntu.
  • Tried to activate and deactivate the simple credentials.
  • Tried with the simple credentials, i.e. username + password.
  • Tried to put my user name in https://username@....
  • Tried to use SSH
  • Tried to use the http.extraHeader in the git command with the header being
    Authorization: Basic Base64Encoded(uname:PAT)
  • Tried to use the http.extraHeader in the git command with the header being
    Authorization: Bearer PAT
  • Tried to use the VSCode client. It generates the PAT by itself but still cannot authenticate and clone.
  • Tried to use the Rider VSTS client. It generates the PAT by itself but still cannot authenticate and clone.
  • Tried to use the git credentials manager for Linux. It promps the Code and when we authenticate in the browser, it generates a PAT successfully. Still, after that cannot authenticate.

Any clue why this is not working?

回答1:

I've experienced the same issue, and spent quite a while searching for a solution. I finally came across this post which contained a solution in the comments section by Martinius79.

In short, it was required to pass the username and PAT, encoded as base64, through git http.extraheaders in order for it to authenticate.

100% Credit to the original author, just including it here to assist others in locating it:

Example: git -c http.extraheader="AUTHORIZATION: Basic TXlHaXRTeW5jVXNlcjo2bHFqNXJkcHEzdXBxZWVmd2o3bDduZXN5NTR3d3gxNHFobDVlanl5NTVkb2g0M3d4YzRh" clone https://tfs.address/tfs/Collection/Project/_git/RepoName

Used basic token BASE64 encoded: TXlHaXRTeW5jVXNlcjo2bHFqNXJkcHEzdXBxZWVmd2o3bDduZXN5NTR3d3gxNHFobDVlanl5NTVkb2g0M3d4YzRh

Basic Token BASE64 decoded: MyGitSyncUser:6lqj5rdpq3upqeefwj7l7nesy54wwx14qhl5ejyy55doh43wxc4a

Token is constructed from : In this example: Fictional user name: MyGitSyncUser Used PAT: 6lqj5rdpq3upqeefwj7l7nesy54wwx14qhl5ejyy55doh43wxc4a

I hope this helps!



回答2:

I have had success using PAT like this;

  1. copy clone url for your repository e.g. git clone https://<domain>.visualstudio.com/<domain>/_git/<domain>
  2. After you have copied you PAT use as;

git clone https://<PAT>@<domain>.visualstudio.com/<domain>/_git/<domain>

Username nor password should be required as the PAT should suffice.



回答3:

Try : First delete the git credentials from Credential manager. Try Generating a new personal access token to resolve this error

Go to security ->Personal access token->Add Give Description, select All scopes, click Create Token.

Copy the token and store it for later use. This token can now be used in place of password for the git user.



回答4:

You need to Base64 encode your token and add it as a HTTP Authorization header. This can be done with OpenSSL:

PAT="enter-your-pat-here"
REPO_URL="https://myorg@dev.azure.com/myorg/myproject/_git/myrepo"

AUTH=$(echo -n ":$PAT" | openssl base64 | tr -d '\n')

git -c http.$REPO_URL.extraHeader="Authorization: Basic $AUTH" clone $REPO_URL

echo -n "..." | ... | tr -d '\n' is a common Linux trick to send a string as input to a command which normally requires user input (try just writing openssl base64 and you'll see).

Note the leading ":" in echo -n ":$PAT". This is because the "user" is missing in the normal format of "user:token".

To avoid retyping the configuration option with each git command you can add the authorization header to your local or global git config:

git config --global http.$REPO_URL.extraHeader "Authorization: Basic $AUTH"

View your changes with:

git config --global --edit


回答5:

This is a known regression in curl 7.61.0, which ships with Ubuntu 18.10. The regression was fixed in curl 7.61.1, but Ubuntu 18.10 doesn't have this fix yet. See (and upvote) https://bugs.launchpad.net/ubuntu/+source/curl/+bug/1805203