Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rotate project access token with custom expires_at#3028

Answeredbynejch
botkero asked this question inQ&A
Discussion options

Hi,

I wanna rotate a project access token but with a customexpires_at.

But I'm not sure how to do inpython-gitlab. Theexample docs don't give any examples and the reference documentation is a bit confusing for me.

I tried following:

>>>type(token)# for reference<class'gitlab.v4.objects.project_access_tokens.ProjectAccessToken'>>>>token.rotate({"expires_at":"2024-12-12"})Traceback (mostrecentcalllast):File"<pyshell#41>",line1,in<module>token.rotate({"expires_at":"2024-12-12"})TypeError:ObjectRotateMixin.rotate()takes1positionalargumentbut2weregiven

Maybe someone can help me out here?

You must be logged in to vote
Answered by nejchOct 30, 2024

@botkero this should already work withtoken.rotate(expires_at="2024-12-12"). Please let us know if not!

See source:

self,id:Union[str,int],expires_at:Optional[str]=None,**kwargs:Any

Replies: 1 comment 3 replies

Comment options

@botkero this should already work withtoken.rotate(expires_at="2024-12-12"). Please let us know if not!

See source:

self,id:Union[str,int],expires_at:Optional[str]=None,**kwargs:Any

You must be logged in to vote
3 replies
@botkero
Comment options

Hmmm, seems not to work for me (tried with4.7.0 and5.0.0).

>>>token.rotate(expires_at="2024-12-12")Traceback (mostrecentcalllast):File"<pyshell#53>",line1,in<module>token.rotate(expires_at="2024-12-12")File"D:\WinPython11\python-3.11.0.amd64\Lib\site-packages\gitlab\mixins.py",line712,inrotateserver_data=self.manager.rotate(self.encoded_id,**kwargs)File"D:\WinPython11\python-3.11.0.amd64\Lib\site-packages\gitlab\exceptions.py",line340,inwrapped_freturnf(*args,**kwargs)File"D:\WinPython11\python-3.11.0.amd64\Lib\site-packages\gitlab\mixins.py",line685,inrotateserver_data=self.gitlab.http_post(path,post_data=data,**kwargs)File"D:\WinPython11\python-3.11.0.amd64\Lib\site-packages\gitlab\client.py",line1000,inhttp_postresult=self.http_request(File"D:\WinPython11\python-3.11.0.amd64\Lib\site-packages\gitlab\client.py",line787,inhttp_requestraisegitlab.exceptions.GitlabAuthenticationError(gitlab.exceptions.GitlabAuthenticationError:401:401Unauthorized
@nejch
Comment options

Hmm, 401 seems more like a permission/authorization issue. Could you please post the entire code snippet withgl.enable_debug() turned on? Just redact any sensitive information.

@botkero
Comment options

Hmm I may have misread the GitLab documentation. I tried to rotate the project access token with a project access token.

Prerequisites:

You must have apersonal access token with the api scope.

Source:Rotate a project access token

If you still curious about the traceback (this time tested on 4.11.1 on different machine):

See here
In [6]:token=project.access_tokens.get(22776)DEBUG:http.client:send: b'GET /api/v4/projects/71699/access_tokens/22776 HTTP/1.1\r\nHost:gitlab.cc-asp.fraunhofer.de\r\nUser-Agent:python-gitlab/4.11.1\r\nAccept-Encoding:gzip,deflate,br\r\nAccept:*/*\r\nConnection:keep-alive\r\nContent-type:application/json\r\nPRIVATE-TOKEN: [MASKED]\r\n\r\n'DEBUG:http.client:reply: 'HTTP/1.1200OK\r\n'DEBUG:http.client:header:Server:nginxDEBUG:http.client:header:Date:Wed,30Oct202419:10:55GMTDEBUG:http.client:header:Content-Type:application/jsonDEBUG:http.client:header:Content-Length:215DEBUG:http.client:header:Connection:keep-aliveDEBUG:http.client:header:Cache-Control:max-age=0,private,must-revalidateDEBUG:http.client:header:Etag:W/"660582192ee29f1c0f996cc5babc5b55"DEBUG:http.client:header:Vary:OriginDEBUG:http.client:header:X-Content-Type-Options:nosniffDEBUG:http.client:header:X-Frame-Options:SAMEORIGINDEBUG:http.client:header:X-Gitlab-Meta: {"correlation_id":"01JBFDD2GH4G5YRDASFKQ8K9SB","version":"1"}DEBUG:http.client:header:X-Request-Id:01JBFDD2GH4G5YRDASFKQ8K9SBDEBUG:http.client:header:X-Runtime:0.053522DEBUG:http.client:header:Referrer-Policy:strict-origin-when-cross-originDEBUG:http.client:header:Strict-Transport-Security:max-age=31536000;includeSubDomainsDEBUG:http.client:header:Set-Cookie:NSC_ESNS=01b1556c-84b6-1722-9678-02006590000d_2939797038_1713146065_00000000000028390579;Path=/;Expires=Wed,30-Oct-202419:11:10GMTDEBUG:http.client:header:Strict-Transport-Security:max-age=31536000DEBUG:urllib3.connectionpool:https://<<gitlab-url>>:443 "GET/api/v4/projects/71699/access_tokens/22776HTTP/1.1"200215In [7]:token.rotate(expires_at="2024-12-12")DEBUG:http.client:send: b'POST /api/v4/projects/71699/access_tokens/22776/rotate HTTP/1.1\r\nHost: <<gitlab-url>>\r\nUser-Agent: python-gitlab/4.11.1\r\nAccept-Encoding:gzip,deflate,br\r\nAccept:*/*\r\nConnection:keep-alive\r\nContent-type:application/json\r\nContent-Length:28\r\nPRIVATE-TOKEN: [MASKED]\r\n\r\n'DEBUG:http.client:send: b'{"expires_at":"2024-12-12"}'DEBUG:http.client:reply: 'HTTP/1.1401Unauthorized\r\n'DEBUG:http.client:header:Server:nginxDEBUG:http.client:header:Date:Wed,30Oct202419:11:22GMTDEBUG:http.client:header:Content-Type:application/jsonDEBUG:http.client:header:Content-Length:30DEBUG:http.client:header:Connection:keep-aliveDEBUG:http.client:header:Cache-Control:no-cacheDEBUG:http.client:header:Vary:OriginDEBUG:http.client:header:X-Content-Type-Options:nosniffDEBUG:http.client:header:X-Frame-Options:SAMEORIGINDEBUG:http.client:header:X-Gitlab-Meta: {"correlation_id":"01JBFDDXQW2R5VDJ96HTJ64CZG","version":"1"}DEBUG:http.client:header:X-Request-Id:01JBFDDXQW2R5VDJ96HTJ64CZGDEBUG:http.client:header:X-Runtime:0.046489DEBUG:http.client:header:Strict-Transport-Security:max-age=31536000;includeSubDomainsDEBUG:http.client:header:Strict-Transport-Security:max-age=31536000DEBUG:urllib3.connectionpool:https://<<gitlab-url>>:443 "POST/api/v4/projects/71699/access_tokens/22776/rotateHTTP/1.1"40130---------------------------------------------------------------------------GitlabAuthenticationErrorTraceback (mostrecentcalllast)CellIn[7],line1---->1token.rotate(expires_at="2024-12-12")File~\Documents\WPy64-31110\python-3.11.1.amd64\Lib\site-packages\gitlab\mixins.py:712,inObjectRotateMixin.rotate(self,**kwargs)710assertisinstance(self.manager,RotateMixin)711assertself.encoded_idisnotNone-->712server_data=self.manager.rotate(self.encoded_id,**kwargs)713self._update_attrs(server_data)File~\Documents\WPy64-31110\python-3.11.1.amd64\Lib\site-packages\gitlab\exceptions.py:344,inon_http_error.<locals>.wrap.<locals>.wrapped_f(*args,**kwargs)341 @functools.wraps(f)342defwrapped_f(*args:Any,**kwargs:Any)->Any:343try:-->344returnf(*args,**kwargs)345exceptGitlabHttpErrorase:346raiseerror(e.error_message,e.response_code,e.response_body)fromeFile~\Documents\WPy64-31110\python-3.11.1.amd64\Lib\site-packages\gitlab\mixins.py:685,inRotateMixin.rotate(self,id,expires_at,**kwargs)682ifexpires_atisnotNone:683data= {"expires_at":expires_at}-->685server_data=self.gitlab.http_post(path,post_data=data,**kwargs)686ifTYPE_CHECKING:687assertnotisinstance(server_data,requests.Response)File~\Documents\WPy64-31110\python-3.11.1.amd64\Lib\site-packages\gitlab\client.py:994,inGitlab.http_post(self,path,query_data,post_data,raw,files,**kwargs)991query_data=query_dataor {}992post_data=post_dataor {}-->994result=self.http_request(995"post",996path,997query_data=query_data,998post_data=post_data,999files=files,1000raw=raw,1001**kwargs,1002 )1003content_type=utils.get_content_type(result.headers.get("Content-Type"))1005try:File~\Documents\WPy64-31110\python-3.11.1.amd64\Lib\site-packages\gitlab\client.py:767,inGitlab.http_request(self,verb,path,query_data,post_data,raw,streamed,files,timeout,obey_rate_limit,retry_transient_errors,max_retries,**kwargs)764pass766ifresult.status_code==401:-->767raisegitlab.exceptions.GitlabAuthenticationError(768response_code=result.status_code,769error_message=error_message,770response_body=result.content,771     )773raisegitlab.exceptions.GitlabHttpError(774response_code=result.status_code,775error_message=error_message,776response_body=result.content,777 )GitlabAuthenticationError:401:401Unauthorized

Thanks for the help and especially the hint on how to useexpires_at.

Answer selected bybotkero
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@botkero@nejch

[8]ページ先頭

©2009-2025 Movatter.jp