I’m trying to push some changes from a script locally.
I already created a consumer for OAuth 2.0 following the Use OAuth on Bitbucket Cloud guide. Already obtain the token from https://bitbucket.org/site/oauth2/access_token
successfully.
But when I try to push my changes (after adding and commit my changes):
git_url = f"https://oauth2:{oauth_token}@bitbucket.org/{my_repository}.git"
result = subprocess.run(["git", "push", "-u", git_url, source_branch], capture_output=True, text=True)
I got this error (token and repo anonymized for security purposes):
(Pdb) result
CompletedProcess(args=['git', 'push', '-u', 'https://oauth2:XXXXXXXXX@bitbucket.org/YYYYYYYYY/ZZZZZZ.git', 'upgrade-version'], returncode=128, stdout='', stderr="remote: Invalid credentials\nfatal: Authentication failed for 'https://bitbucket.org/YYYYYYYYY.git/'\n")
To ensure I’m not using the wrong repository I directly copied and pasted from navigator by clicking in clone report and contains Workspace+repository name
To obtain the OAuth Token:
def obtain_oauth_token(client_id, client_secret):
# Bitbucket OAuth token endpoint
token_url = "https://bitbucket.org/site/oauth2/access_token"
# OAuth token request parameters
data = {
"grant_type": "client_credentials"
}
# OAuth client credentials for authentication
auth = (client_id, client_secret)
try:
# Send POST request to obtain OAuth token
response = requests.post(token_url, data=data, auth=auth)
# Check if request was successful
if response.status_code == 200:
# Parse JSON response and extract access token
token_data = response.json()
access_token = token_data.get("access_token")
return access_token
else:
error_data = response.json()
print(f"Failed to obtain OAuth token: <{response.status_code}> {error_data['error_description']}")
return None
except Exception as e:
print("Error:", e)
return None
Also, I tried to:
command = ["git", "remote", "add", "oauth", f"https://oauth2:{oauth_token}@{repository_url}"]
result = subprocess.run(command, capture_output=True, text=True)
and later push using oauth:
result = subprocess.run(["git", "push", "oauth", source_branch], capture_output=True, text=True)
But I’m having same issue than before:
(Pdb) result
CompletedProcess(args=['git', 'push', 'oauth', 'upgrade-version'], returncode=128, stdout='', stderr="remote: Invalid credentials\nfatal: Authentication failed for 'https://bitbucket.org/YYYYYYYYY/ZZZZZZ.git/'\n")
Also I tried with https://x-token-auth:{oauth_token}@bitbucket.org/{workspace-id}/{repo}.git
:
git_url = f"https://x-token-auth:{oauth_token}@bitbucket.org/{repository}.git"
result = subprocess.run(["git", "push", "oauth", source_branch], capture_output=True, text=True)
But it didn’t work either
My internal variable repository
contains the workspace and repo.
Can someone help me on this?