Python: How do I resolve oauth2client.client.ApplicationDefaultCredentialsError: The Application Default Credentials are not available?

Deborah Swanson python at deborahswanson.net
Sun Feb 5 14:58:09 EST 2017


OK David,

I'm also a Linux newbie and my Linux PC has a dead power supply that
I've been too sick to fix. Since it is a choice (given my limited energy
resources), I've spent my time learning python on a very old Windows PC
instead of reviving the Linux PC, and I've never used Google Apps.

However, looking at the links provided, this is how I'd proceed:

1) At
https://developers.google.com/identity/protocols/application-default-cre
dentials (Google Application Default Credentials  |  Google Identity
Platform), are the insructions you need to follow.

Now, I think you make up your own path and filename for the environment
variable, the more iffy question is what file it shoud be.

And the answer appears to be that it's a file you download. It then
tells you to follow several instructions, using the
https://console.developers.google.com/projectselector/apis/credentials
page, to get a file that automatically downloads to your computer.

2) It then says you have to make the environment variable
GOOGLE_APPLICATION_CREDENTIALS, specifying that it points to the file
you just downloaded. 

I don't have a working Linux machine to test this on, at least it sounds
like you're using Linux (let me know if you're using Windows and I can
test it there), but to permanently set an environment variable in Linux:

Edit ~/.bashrc and add the line:

export GOOGLE_APPLICATION_CREDENTIALS=<path/filename>

(instructions are different for Windows, or Linux using something other
than bash for shell scripts)

That's what you need to do, and then your youtube_search.py should work
(haha, well you can try it and see).

Deborah

David Amadi wrote, on February 05, 2017 9:38 AM
> 
> Hi Deborah,
> 
> 
> Thank you for stepping in, I appreciate it.
> 
> 
> Here's the python code I'm working with - 
> http://codegists.com/snippet/python/youtube_searchpy_valeraradu_python
> 
> 
> To be honest, I don't know how to define the environment 
> variable GOOGLE_APPLICATION_CREDENTIALS to point to a file 
> defining the credentials. In the python script (link in the 
> above line), you could only add the API Key, which I obtained 
> and provided - but the script won't run.
> 
> 
> I look forward to hearing back from you.
> 
> 
> Regards,
> David
> 
> 
> On 5 February 2017 at 03:16, Deborah Swanson 
> <python at deborahswanson.net> wrote:
> 
> david.amadi at digital.beis.gov.uk wrote, on
> Saturday, February 04, 2017 3:39 PM
> >
> > Hello All,
> >
> > I'm a newbie to python programming - got into it
> 
> > predominately for the purposes of machine learning and data 
> mining and 
> > even though I've committed several weeks to learning the scripting 
> > language, I have struggled to fully grasp how it works.
> >
> > I'm looking to scrape title, video Id, view Count, like 
> Count, dislike 
> > Count, comment Count, favourite Count etc off YouTube using 
> a python 
> > script I found via an online tutorial.
> >
> > I have installed 'unidecode' and 'google-api-python-client' 
> packages 
> > via my terminal. I have also enabled YouTube Data Api V3 and I'm 
> > getting the error below each time I run the script.
> >
> > Could anyone please point me in the right direction?
> >
> > Thanks a lot in advance for your help
> >
> > ****
> > Traceback (most recent call last):
> >   File "/Users/*Path*/ML with Python/youtube_search.py", 
> line 73, in 
> > <module>
> >     youtube_search(args)
> >   File "/Users/*Path*/ML with Python/youtube_search.py", 
> line 21, in 
> > youtube_search
> >     youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, 
> > developerKey=DEVELOPER_KEY)
> >   File 
> "/Users/*Path*/anaconda/lib/python3.5/site-packages/oauth2clie
> > nt/_helpers.py", line 133, in positional_wrapper
> >     return wrapped(*args, **kwargs)
> >   File
> > "/Users/*Path*/anaconda/lib/python3.5/site-packages/googleapic
> > lient/discovery.py", line 226, in build
> >     credentials=credentials)
> >   File
> > "/Users/*Path*/anaconda/lib/python3.5/site-packages/oauth2clie
> > nt/_helpers.py", line 133, in positional_wrapper
> >     return wrapped(*args, **kwargs)
> >   File
> > "/Users/*Path*/anaconda/lib/python3.5/site-packages/googleapic
> 
> > lient/discovery.py", line 358, in build_from_document
> >     credentials = _auth.default_credentials()
> >   File 
> "/Users/*Path*/anaconda/lib/python3.5/site-packages/googleapic
> > lient/_auth.py", line 41, in default_credentials
> >     return
> > oauth2client.client.GoogleCredentials.get_application_default()
> >   File
> > "/Users/*Path*/anaconda/lib/python3.5/site-packages/oauth2clie
> > nt/client.py", line 1264, in get_application_default
> >     return GoogleCredentials._get_implicit_credentials()
> >   File
> > "/Users/*Path*/anaconda/lib/python3.5/site-packages/oauth2clie
> > nt/client.py", line 1254, in _get_implicit_credentials
> >     raise ApplicationDefaultCredentialsError(ADC_HELP_MSG)
> > oauth2client.client.ApplicationDefaultCredentialsError: The
> > Application Default Credentials are not available. They are
> > available if running in Google Compute Engine. Otherwise, the
> > environment variable GOOGLE_APPLICATION_CREDENTIALS must be
> > defined pointing to a file defining the credentials. See
> > https://developers.google.com/accounts/docs/application-defaul
> t-credentials for more information.
> 
> --
> Communications with the Department for Business, Innovation 
> and Skills may be automatically logged, monitored and/or 
> recorded for legal purposes.
> --
> 
> I'm also a relatively new python coder (little over a year), 
> but it seems plain to me that "The Application Default 
> Credentials ... are available if running in Google Compute 
> Engine. Otherwise, the environment variable 
> GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a 
> file defining the credentials. See 
> https://developers.google.com/accounts/docs/application-defaul
t-credenti
als for more information."

If you're not running in Google Compute Engine, you need to make a file
defining the credentials, and supposedly there are instructions or
requirements in the link above.

Looks like Anaconda tried to get the credentials by calling its
_auth.default_credentials() function:

credentials = _auth.default_credentials()

from "python3.5/site-packages/googleapiclient/discovery.py", but it
failed. I think you need to read that link and make the file it wants to
find.

Deborah







-- 

David Amadi

Digital Performance Analyst | Apprenticeships Service Department for
Business, Energy and Industrial Strategy


david.amadi at digital.beis.gov.uk


M. 07904776300

Communications with the Department for Business, Innovation and Skills
may be automatically logged, monitored and/or recorded for legal
purposes.


Communications with the Department for Business, Innovation and Skills
may be automatically logged, monitored and/or recorded for legal
purposes.




More information about the Python-list mailing list