googleapis / google-cloud-ruby

Google Cloud Client Library for Ruby
https://googleapis.github.io/google-cloud-ruby/
Apache License 2.0
1.35k stars 546 forks source link

RuntimeError(Could not load the default credentials) #4741

Closed arslanmurtaza closed 4 years ago

arslanmurtaza commented 4 years ago

Environment details

Steps to reproduce

  1. Not able to configure as stated in documentation: https://googleapis.dev/ruby/google-cloud-monitoring/latest/file.AUTHENTICATION.html

Code example

Google::Cloud::Monitoring.configure do |config|
  config.project_id  = project_id
  config.credentials = keyfile
end

Full backtrace

/initializers/google_cloud.rb:34:in `<top (required)>': undefined method `configure' for Google::Cloud::Monitoring:Module (NoMethodError)

I'm using the Stackdriver logging, Bigquery and error reporting too. Those are working correctly with the following configuration

Google::Cloud.configure do |config|
  config.error_reporting.project_id = project_id
  config.error_reporting.keyfile = keyfile

  config.logging.project_id = project_id
  config.logging.keyfile = keyfile

  config.bigquery.project_id = project_id
  config.bigquery.keyfile  = keyfile
end

I tried with

config.monitoring.project_id = project_id

too but that also gives the error like:

/config/initializers/google_cloud.rb:21:in `block in <top (required)>': undefined method `project_id=' for nil:NilClass (NoMethodError)

Any help will be greatly appreciated. Thanks!

quartzmo commented 4 years ago

@arslanmurtaza Hi and thanks for opening this issue!

It appears that the documentation showing the use of configure in AUTHENTICATION is incorrect for purely generated libraries. google-cloud-error_reporting, google-cloud-error_reporting and google-cloud-error_reporting all have a manually-written layer that exposes settings via configure, but google-cloud-monitoring is purely generated and does not. I'll open a separate issue to fix the documentation.

Meanwhile, I'd like to help you get past your authentication issue. Is there a reason why you can't use environment variables to set your credentials, as correctly shown in the first two sections of AUTHENTICATION and below? Then you can create monitoring clients with no arguments, and the credentials will be discovered automatically:

export MONITORING_CREDENTIALS=/path/to/json

or for all APIs:

export GOOGLE_CLOUD_CREDENTIALS =/path/to/json
irb(main):001:0> require "google/cloud/monitoring"
=> true
irb(main):002:0> client = Google::Cloud::Monitoring::ServiceMonitoring.new
irb(main):003:0> client
=> #<Google::Cloud::Monitoring::V3::ServiceMonitoringServiceClient:0x00007fec1aa116d0
arslanmurtaza commented 4 years ago

@quartzmo I actually tried that too, It was giving me the below error:

RuntimeError (Could not load the default credentials. Browse to)
https://developers.google.com/accounts/docs/application-default-credentials
for more information

Complete trace is

➜  ~ export MONITORING_CREDENTIALS="~/Downloads/my-keyfile.json"
➜  ~ irb
2.6.5 :001 > require "google/cloud/monitoring"
 => true 
2.6.5 :002 > client = Google::Cloud::Monitoring::ServiceMonitoring.new
Traceback (most recent call last):
       11: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/bin/irb:23:in `<main>'
       10: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/bin/irb:23:in `load'
        9: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        8: from (irb):2
        7: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-monitoring-0.34.2/lib/google/cloud/monitoring.rb:416:in `new'
        6: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-monitoring-0.34.2/lib/google/cloud/monitoring/v3.rb:455:in `new'
        5: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-monitoring-0.34.2/lib/google/cloud/monitoring/v3.rb:455:in `new'
        4: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-monitoring-0.34.2/lib/google/cloud/monitoring/v3/service_monitoring_service_client.rb:182:in `initialize'
        3: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/googleauth-0.10.0/lib/googleauth/credentials.rb:295:in `default'
        2: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/googleauth-0.10.0/lib/googleauth/credentials.rb:326:in `from_application_default'
        1: from /Users/arslanmurtaza/.rvm/rubies/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/googleauth-0.10.0/lib/googleauth/application_default.rb:76:in `get_application_default'
RuntimeError (Could not load the default credentials. Browse to)
https://developers.google.com/accounts/docs/application-default-credentials
for more information

Also I need to keep the configuration consistent if possible.

quartzmo commented 4 years ago

Can you try export MONITORING_CREDENTIALS=/Users/arslanmurtaza/Downloads/my-keyfile.json instead of export MONITORING_CREDENTIALS="~/Downloads/my-keyfile.json"?

And if that doesn't work, try moving my-keyfile.json to your home dir and use: export MONITORING_CREDENTIALS=/Users/arslanmurtaza/my-keyfile.json? (I think maybe by default OS X Terminal no longer allows access to Downloads?)

arslanmurtaza commented 4 years ago

Ah right, that worked.

export MONITORING_CREDENTIALS=/Users/arslanmurtaza/Downloads/my-keyfile.json

It was the Mac OS Catalina which was not letting the terminal read the credentials. Thanks alot.