ip2k / google-api-ruby-client

Automatically exported from code.google.com/p/google-api-ruby-client
Apache License 2.0
0 stars 0 forks source link

Working Oauth example? #10

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi All,

It looks like the Google Buzz example given has some differences from other 
APIs.  I've been struggling with this for a while trying to get it to 
authenticate for the Prediction API, and no luck yet.  Does anyone have a 
working example of this??

Here is what I have so far:

    client = Google::APIClient.new(:authorization => :oauth_1)
    client.authorization.client_credential_key = "MY_CLIENT_KEY"
    client.authorization.client_credential_secret = "MY_CLIENT_SECRET"

    client.authorization.fetch_temporary_credential!(
      :additional_parameters => {
        'scope' => 'https://www.googleapis.com/auth/prediction'
      }
    )

    verifier = nil
    if verifier.nil?
      redirect_uri = client.authorization.authorization_uri(
        :additional_parameters => {
          'domain' => client.authorization.client_credential_key,
          'scope' => 'https://www.googleapis.com/auth/prediction'
        }
      )
      puts "Please go visit this URL in your browser and copy the new verifier!", redirect_uri
      return
    else
      client.authorization.fetch_token_credential!(:verifier => verifier)
    end

    prediction = client.discovered_api('prediction', 'v1.2')

    # Make an API call
    response = client.execute(prediction.training.get, {'data' => "#{MY_BUCKET}/#{MY_FILE}", 'key' => "MY_API_KEY"})
    puts response.to_yaml

After calling it the first time I go to the given url in my browser and allow 
it, then copy/paste the given verifier code into the 'verifier' variable.

Rerunning it produces the following error when it calls 
`fetch_token_credential!`:

gems/signet-0.2.2/lib/signet/oauth_1/client.rb:803:in `fetch_token_credential': 
Authorization failed.  Server message: (Signet::AuthorizationError)
The token is invalid.

Original issue reported on code.google.com by barmstrong@gmail.com on 17 Jun 2011 at 10:37

GoogleCodeExporter commented 9 years ago
Are you able to run the command line client? I have a similar issue but I get 
"Missing Access Token" error. I also get this error when running the command 
line client too.

http://code.google.com/apis/predict/docs/general_discussion_forum.html?place=msg
%2Fprediction-api-discuss%2Ff1hYzq9QVIA%2FYycntxMhksUJ

Original comment by adambro...@gmail.com on 18 Jun 2011 at 12:14

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Adam, you should CC the google-api-ruby-client mailing list in the future — I 
can't read the forums for all APIs, just the client one, and same goes for all 
the other client maintainers.

I'll take a look and see if I can duplicate the issue you're having.

However, I'd recommend using OAuth 2 whenever you can. It's much better 
supported at this point and a lot easier to use. Plus the user experience is 
way better.

Original comment by sporkmonger on 18 Jun 2011 at 1:21

GoogleCodeExporter commented 9 years ago
Do this instead:
bin/google-api oauth-2-login \
   --scope=https://www.googleapis.com/auth/prediction \
  --client-id=<your-client-id> \
  --client-secret=<your-client-secret>

Then you can make your API calls:
google-api execute prediction.training.insert -- data=bucket/train.csv

Original comment by bobaman@google.com on 18 Jun 2011 at 4:03

GoogleCodeExporter commented 9 years ago
Also note that the command line tool does not currently handle token 
expirations gracefully, so you'll need to reauth the client manually. It's not 
intended for usage in scripts. You should use the client directly for that.

Original comment by bobaman@google.com on 18 Jun 2011 at 4:05

GoogleCodeExporter commented 9 years ago
To barmstrong's original issue, did you URL unescape the verifier? Verifier 
codes pulled from the URL will usually be percent encoded.

Original comment by bobaman@google.com on 18 Jun 2011 at 4:07

GoogleCodeExporter commented 9 years ago
No response from original reporter. Closing.

Original comment by bobaman@google.com on 6 Oct 2011 at 9:42