1

I'm using the google-api-client client gem for ruby and getting a 403 Access Not Configured error whenever I call the API.

  require 'google/api_client'
  client = Google::APIClient.new
  client.authorization = nil
  search = client.discovered_api('customsearch')
  response = client.execute( search.cse.list, 'key' => '<<MY KEY>>', 'cx' => '<<MY CX>>', 'alt' => 'json', 'q' => 'hello world')

I'm trying to search without using OAuth, and just the API key.

Any help would be appreciated. Thank you!

user749798
  • 5,210
  • 10
  • 51
  • 85

2 Answers2

1

I had the same problem and I solved it like this:

Google Account Setup:

  1. Go to Google Apis (https://code.google.com/apis/console/).
  2. Under "Services" enable the service that you would like to use (e.g. Analytics API).
  3. Under "API Access" create a OAuth Client ID by clicking on the big blue button.
  4. On the next screen enter your product information (name, logo, url).
  5. On the next screen, because the communication will handled by the server, we check the "Service Account" option and click the Create Client ID button.
  6. Download the private key that was just generated and put it to ({Rails.root}/config/.p12).
  7. Leave the API Access page opened because you will need some parameter values in the next step.
  8. In new tab, go to https://developers.google.com/products/ and choose the service that you would like to use. In the left menu navigate to Configuration > Management API (v3) > Resources > Authorization (https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization). In the table below you will find the appropriate ":scope" parameter url (e.g. https://www.googleapis.com/auth/analytics.readonly). This is how you find the appropriate scope.
  9. Note that the ":issuer" parameter is the email address of the service account that's displayed next to the Client ID (e.g. @developer.gserviceaccount.com).
  10. For the checking the URL (e.g. https://developers.google.com/youtube/analytics/v1/ would be youtubeAnalytics, v1). I usualy find that googling examples :).
  11. Note that the is the "Public key fingerprints" value.
  12. Do check the source code at https://github.com/google/google-api-ruby-client for each object returned.
  13. Maybe you will have to add the issuer email address to your Google Analytics Account.

Ruby on Rails Code:

# creating client instance
client = Google::APIClient.new

# authenticating
key = Google::APIClient::PKCS12.load_key("#{Rails.root}/config/<STRANGE_LONG_FILENAME>.p12", 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
  :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
  :audience => 'https://accounts.google.com/o/oauth2/token',
  :scope => '<SCOPE_URL>',
  :issuer => '<HASH>@developer.gserviceaccount.com',
  :signing_key => key)
client.authorization.fetch_access_token!

# API call
# NOTE: Check the documentation for API methods and parameters). The method discovered_api returns a service object. We can use to_h.keys to get the list of available keys of that object. Keys represents API methods (e.g. "analytics.management.accounts.list" the API method path is "management.accounts.list").
result = client.execute(
  :api_method => client.discovered_api('<SERVICE_NAME>', 'v3').management.accounts.list,
  :parameters => { accountId: '~all', webPropertyId: '~all'}
)
if result.success?
    result.data
end
xpepermint
  • 35,055
  • 30
  • 109
  • 163
0

Access not configured means that it's an API that requires your project to be registered in the API console, with the custom search API turned on from the services tab. Your client is either identified by an OAuth token or an API key. One or both must be supplied. I suspect you've just failed to turn the API on in the services tab, since your example includes the API key parameter.

You might also try being more explicit in passing your parameters:

result = client.execute(
  :api_method => search.cse.list,
  :key => '<<MY KEY>>',
  :parameters => {
    'cx' => '<<MY CX>>',
    'alt' => 'json',
    'q' => 'hello world'
  }
)
Bob Aman
  • 32,839
  • 9
  • 71
  • 95
  • thanks, though the key the services tab still has it turned on. I'm also using the "Simple API Access" Web Browser key. If I use the code for one of their generated engines it works, but this won't for some reason. – user749798 Jan 02 '13 at 17:40
  • Not sure... maybe try using a service account instead? – Bob Aman Jan 07 '13 at 18:48