Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use with oauth2 #89

Closed
olihey opened this issue Aug 17, 2017 · 10 comments
Closed

Use with oauth2 #89

olihey opened this issue Aug 17, 2017 · 10 comments
Assignees

Comments

@olihey
Copy link

olihey commented Aug 17, 2017

I started playing around with resty and liking it so far.
I am talking to a REST API that uses oauth2 and up until now using net/http and golang/oauth2 worked but resty is much nicer.

My problem is refreshing the accessToken for oauth when it's expired. By using the oauth2.Client it's very simple but unfortunately setting the http.Client is not possible in resty.

So, 2 questions:

  • Is there a way to use oauth2.Client with resty
  • Is there an elegant way to handle expiring accessTokens in resty apart from checking the expire date before each call and refresh the token
@jeevatkm jeevatkm self-assigned this Aug 17, 2017
@jeevatkm
Copy link
Member

Currently no easy way to do it. @bak1an has finished the implementation of http.RoundTripper interface as a transport. It seems he is busy. I will take it to that closure.

Once #74 get merged into main track you can integrate oauth very seamlessly.

@jeevatkm jeevatkm added this to the v1.0 Milestone milestone Aug 17, 2017
@jeevatkm
Copy link
Member

I'm working on it. Let you know once done. Thanks.

@jeevatkm
Copy link
Member

http.RoundTripper implementation is on this branch https://github.com/go-resty/resty/tree/bak1an-roundtripper

Please give it try and share your feedback then I will merge it to master.

For example:

// Create oauth client 
// ...
oauthClient := conf.Client(ctx, token)

// Create resty client
restyClient := resty.New()

// http://godoc.org/golang.org/x/oauth2 implements `httpRoundTripper` interface
// Set the oauthClient transport
restyClient.SetTransport(oauthClient.Transport)

// Now you can use resty :)

@jeevatkm
Copy link
Member

Solved by #91

@olihey
Copy link
Author

olihey commented Aug 18, 2017

Tested and it works beautifully. Thanks and awesome job, really loving resty!!!!

@olihey olihey closed this as completed Aug 18, 2017
@jeevatkm
Copy link
Member

I'm very glad, thank you for your appreciation.

For this enhancement I gave only final touch. Credit goes to @bak1an for the PR.

@aeneasr
Copy link

aeneasr commented Sep 27, 2017

This does not work because SetTransport expects *http.Transport but oauth2.Config.Client().Transport implements *oauth2.Transport which in turn is incompatible with *http.Transport. Why isn't it possible to set the http.Client in resty? Wouldn't that resolve many issues around e.g. retry and also be the idiomatic Go way?

I'm on the v0 branch.

@jeevatkm
Copy link
Member

@arekkas please use v1 or master (edge) for this feature. It is recently added. Thank you.

Can you please explain? What is the issue you're facing with retry? http.Client does not provide retry option; resty provides backoff mechanism and conditional option too.

@aeneasr
Copy link

aeneasr commented Sep 27, 2017

That was a quick response, awesome! :) I see, then I'll upgrade to v1. I think using http.RoundTripper is already all I need, thank you!

@jeevatkm
Copy link
Member

@arekkas you're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants