Created
November 20, 2019 20:19
-
-
Save egemenzeytinci/ecdecee456af0492707d07eeb5d467eb to your computer and use it in GitHub Desktop.
Connection with service accounts for Google APIs with "ImportError: cannot import name SignedJwtAssertionCredentials" error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from oauth2client.service_account import ServiceAccountCredentials | |
import httplib2 | |
import os | |
SCOPE = 'API_SCOPE' | |
ACCOUNT = 'SERVICE_ACCOUNT' | |
KEY = 'KEY_PATH' | |
def initialize(): | |
""" | |
Get credentials | |
:return: credentials | |
:rtype: oauth2client.service_account.ServiceAccountCredentials | |
""" | |
cred = None | |
args = { | |
'service_account_email': ACCOUNT, | |
'filename': KEY, | |
'scopes': SCOPE, | |
} | |
if os.path.exists(KEY): | |
cred = ServiceAccountCredentials.from_p12_keyfile(**args) | |
return cred | |
def connect(cred): | |
""" | |
Connect to API | |
:param ServiceAccountCredentials cred: credentials | |
:return: api connection | |
:rtype: httplib2.Http | |
""" | |
http = httplib2.Http() | |
if cred: | |
cred.refresh(http) | |
http = cred.authorize(http) | |
else: | |
print('Credentials not found!') | |
return http | |
def main(): | |
cred = initialize() | |
conn = connect(cred) | |
print(conn) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment