Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k 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 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...