Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - Access to Google API without using a json file

Is there a way to avoid using the client_secret.json file and use variables from it by declaring them programmatically in the project setup files? I mean instead of using

  KEY_FILE_LOCATION = '/home/user/ga/client_secrets.json'
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

smth like this settings.py

CLIENT_CONFIG = {
    ....
    'client_id': settings.GOOGLE_CLIENT_ID,
    'project_id': settings.GOOGLE_PROJECT_ID,
    'private_key': settings.GOOGLE_CLIENT_SECRET,
    .....

    }

main.py

  from settings import CLIENT_CONFIG
    
  credentials = ServiceAccountCredentials.SOME_FUNCTION(
      CLIENT_CONFIG, SCOPES)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like it can be done by using the function from_json_keyfile_dict https://github.com/googleapis/oauth2client/blob/50d20532a748f18e53f7d24ccbe6647132c979a9/oauth2client/service_account.py#L226 passing in the contents of the json file as a variable

data = {
...
json file content
....
}
credentials = ServiceAccountCredentials.from_json_keyfile_dict(
      data, SCOPES)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...