dblock / strava-ruby-client

A complete Ruby client for the Strava API v3.
https://code.dblock.org/2018/11/27/writing-a-new-strava-api-ruby-client.html
MIT License
97 stars 22 forks source link

new authentication.... #20

Closed benny59 closed 5 years ago

benny59 commented 5 years ago

Hi, I wrote a little "private" application to download my data and put them on a .xls file and .klm (no web) using strava-api-v3. Now it is not working animore.

I read strava instructions and I am very confused about what I have to do to move on in terms of conversion.

I also have found strava-ruby-client gem more recently updated and I tried to restart from scratch (ultimately I need an array with my data). Can you please post a little example on how the workflow process can be implemented?

here is my code:

client = Strava::OAuth::Client.new( client_id: "19", client_secret: "f8*****e677" ) puts client redirect_url = client.authorize_url( client_id: "1*9", client_secret: "f8*****e677", redirect_uri: 'localhost', approval_prompt: 'auto', response_type: 'code', scope: 'activity:read_all' ) puts redirect_url response = client.oauth_token()

I get this error: /strava-ruby-client-0.3.1/lib/strava/web/raise_error.rb:15:in `on_complete': Bad Request (Strava::Errors::Fault)

thanks in advance benny

dblock commented 5 years ago

The OAuth process is web-based, so now you can't just get a token and use it, you have to follow redirects. So you are starting correctly, but that redirect URL needs to be pointing to a local web server that you start and run and which is called by a Strava OAuth redirect.

If you want to do it all in Ruby, check out https://github.com/dblock/strava-ruby-client/blob/master/bin/strava-oauth-token which is a full working example.

Another tool that implements the same workflow is https://github.com/dblock/strava-ruby-cli, you could maybe just extend that to export activities and save them as CSV/XLS/etc.

I'll close this for now, but please add more questions here if you need to and I'll do my best to help.

benny59 commented 5 years ago

Hi Daniel, thanks for your quick reply.

I may want to proceed with the mentioned .kml development as a redirect is required. This application was for myself only and I use it to color in blue already ridden roads and plan routes over still "white" roads. I manually copy the generated .kml file from time to time here: http://benny59.altervista.org/showKml3.htm

maybe with some more effort I can develop it to make it available to other users, if I understand what the new authorization methods intend to do....

Any suggestion will be welcome regards Marco

benny59 commented 5 years ago

Your suggestion and example was usefull. It worked, thanks

dblock commented 5 years ago

I added a section to the doc.

chino commented 8 months ago

So are you saying that you can't just take the Your Access Token value from https://www.strava.com/settings/api and pass that to Strava::Api::Client.new(access_token: ...) ?

I tried doing that and it apparently was able to successfully print client.athlete but throws an auth error for other things like activities: {"resource"=>"AccessToken", "field"=>"activity:read_permission", "code"=>"missing"}

Also I was getting ssl errors until I commented out options[:ssl] in: strava-ruby-client-2.0.0/lib/strava/web/connection.rb ?

dblock commented 8 months ago

So are you saying that you can't just take the Your Access Token value from https://www.strava.com/settings/api and pass that to Strava::Api::Client.new(access_token: ...) ?

@chino Yes, you should be able to. And you saw it work for athlete. If it cannot get this athlete's activities, then it looks like a limitation of the token permissions in the Strava API. Either way the token in the UX is short lived and needs to be refreshed within some hours, so it's not a viable long term solution, you'll want to do an OAuth workflow.

Also I was getting ssl errors until I commented out options[:ssl] in: strava-ruby-client-2.0.0/lib/strava/web/connection.rb ?

You should open a separate issue on that with details, likely some ruby/ssl version problem.