Thread safe of httplib2 and credentials -> update Documentation
See original GitHub issueSo right now I’m using your library with multiple threads. Due to the fact that httplib2 is not thread safe I tried to use your examples.
# Create a new Http() object for every request
def build_request(http, *args, **kwargs):
new_http = httplib2.Http()
return apiclient.http.HttpRequest(new_http, *args, **kwargs)
service = build('api_name', 'api_version', requestBuilder=build_request)
# Pass in a new Http() manually for every request
service = build('api_name', 'api_version')
http = httplib2.Http()
service.stamps().list().execute(http=http)
But either way I have the problem that the threads are not authenticated. In the documentation is nothing like this mentioned.
I tried just building an own httplib2.Http()
object for every thread at the beginning of it and also tried the approach of overriding the default requestBuilder
of the service which I used across all threads but nothing worked due to authentification issues.
In the end it worked for me that I just executed the following authenticate_and_build_service()
function for every thread. This function basically return a new service
object via the build('drive', 'v3', credentials=creds)
.
def authenticate_and_build_service():
# If modifying these scopes, delete the file server_token.pickle.
scopes = ['https://www.googleapis.com/auth/drive']
creds = None
# The file server_token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('server_token.pickle'):
with open('server_token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'server_credentials.json', scopes)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('server_token.pickle', 'wb') as token:
pickle.dump(creds, token)
return build('drive', 'v3', credentials=creds)
Nevertheless I’m not sure if it has any downsides of building a service
object for every thread and it would be nice if someone could comment on this and maybe even update the documentation.
I also didn’t find a way in the execute()
function to specify credentials.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top GitHub Comments
@DopeforHope Thanks for the detailed report! It looks like our docs need to be updated to explain how to authorize the httplib2 instances.
After digging up the source code, I found this in
_auth.py
:So I think I can just do something like this:
UPDATE: This works for me. Although I’d still love to hear from google engineers if this is a recommended approach