Set GOOGLE_APPLICATION_CREDENTIALS in Python project to use Google API

后端 未结 4 1493
死守一世寂寞
死守一世寂寞 2020-12-23 09:42

I am a programming beginner and thing I\'m trying to learn how to use google API with Python.

I have:

  1. created a project on Google Cloud an
相关标签:
4条回答
  • 2020-12-23 09:52

    If you're working on a jupyter notebook and want to set GOOGLE_APPLICATION_CREDENTIALS environment variable in Python code :

    import os
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/path/to/file.json"
    
    0 讨论(0)
  • 2020-12-23 09:52

    I know that this post was answered but the following is a cleaner way to specify the GOOGLE_APPLICATION_CREDENTIALS variable.

    client = language.LanguageServiceClient.from_service_account_json("/path/to/file.json")
    
    0 讨论(0)
  • 2020-12-23 09:54

    There is 1 more simpler way of making it working by explicity mentioning Credentials and passing them to client as shown below.

        import os
        from google.oauth2 import service_account
    
        credentials = service_account.Credentials.from_service_account_file("your-json- 
        file-path-with-filename.json")
        client = language.LanguageServiceClient(credentials=credentials)
    
    0 讨论(0)
  • 2020-12-23 10:14

    Another way to test this is to go into the terminal and type:

    # Linux/Unix
    set | grep GOOGLE_APPLICATION_CREDENTIALS 
    

    or

    # Windows:
    set | Select-String -Pattern GOOGLE_APPLICATION_CREDENTIALS 
    

    This will show you the environment variable and the path where it is located. If this returns nothing then you have not set the variable or you may have the wrong path set

    0 讨论(0)
提交回复
热议问题