Skip to content

Commit 3164a0b

Browse files
committed
Fix OAuth2 authentication support
1 parent f8080db commit 3164a0b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

docs/api-usage.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To connect to a GitLab server, create a ``gitlab.Gitlab`` object:
2525
gl = gitlab.Gitlab('http://10.0.0.1', 'JVNSESs8EwWRx5yDxM5q')
2626
2727
# oauth authentication
28-
gl = gitlab.Gitlab('http://10.0.0.1', my_oauth2_token)
28+
gl = gitlab.Gitlab('http://10.0.0.1', oauth_token='my_oauth2_token')
2929
3030
# or username/password authentication
3131
gl = gitlab.Gitlab('http://10.0.0.1', email='jdoe', password='s3cr3t')

gitlab/__init__.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def set_token(self, token=None, oauth_token=None):
358358

359359
if oauth_token:
360360
self.headers.pop("PRIVATE-TOKEN", None)
361-
self.headers["Authorization"] = "Bearer: %s" % oauth_token
361+
self.headers["Authorization"] = "Bearer %s" % oauth_token
362362
elif token:
363363
self.headers.pop("Authorization", None)
364364
self.headers["PRIVATE-TOKEN"] = token
@@ -397,16 +397,22 @@ def _raw_get(self, path_, content_type=None, streamed=False, **kwargs):
397397
url = '%s%s' % (self._url, path_)
398398

399399
headers = self._create_headers(content_type)
400+
401+
if 'Authorization' in self.headers:
402+
auth = None
403+
else:
404+
auth = requests.auth.HTTPBasicAuth(
405+
self.http_username,
406+
self.http_password)
407+
400408
try:
401409
return self.session.get(url,
402410
params=kwargs,
403411
headers=headers,
404412
verify=self.ssl_verify,
405413
timeout=self.timeout,
406414
stream=streamed,
407-
auth=requests.auth.HTTPBasicAuth(
408-
self.http_username,
409-
self.http_password))
415+
auth=auth)
410416
except Exception as e:
411417
raise GitlabConnectionError(
412418
"Can't connect to GitLab server (%s)" % e)
@@ -438,7 +444,7 @@ def _raw_list(self, path_, cls, extra_attrs={}, **kwargs):
438444
results = [cls(self, item, **params) for item in r.json()
439445
if item is not None]
440446
if ('next' in r.links and 'url' in r.links['next']
441-
and get_all_results is True):
447+
and get_all_results is True):
442448
args = kwargs.copy()
443449
args['next_url'] = r.links['next']['url']
444450
results.extend(self.list(cls, **args))

0 commit comments

Comments
 (0)