-
Notifications
You must be signed in to change notification settings - Fork 58
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
Support OAuth API endpoints #12
Conversation
oauth_session.go
Outdated
return session, nil | ||
} | ||
|
||
func (s *OAuthSession) newToken(postValues *url.Values) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was originally broken up into a separate method with the intent of it being used by whatever does re-authorization of tokens. Or however we decide on implementing implicit grant authorization.
Thanks for the WIP. I just got back from CoreOS Fest and am quite jetlagged, so you'll have to give me a bit of time to read over everything and generate a response. |
Not a problem. I will leave comments if I make further changes. |
oauth_request.go
Outdated
@@ -0,0 +1,53 @@ | |||
package geddit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copyright
Looks really good so far 👍 |
@aggrolite any progress here? I've also noticed there's this library which should prove extremely useful for this |
@jzelinskie I will update the patch soon. Thanks for linking the oauth library. |
I'm back on this. Sorry for the delay. I'm hoping to crank out some code during Xmas. |
Cool -- no rush, get it done in your leisure. Have a great holiday ⛄ |
e766f04
to
8b42116
Compare
The OAuth library really simplified the code. Thanks for pointing it out. Everything's still WIP, but I have changes to Next TODO's include: adding more endpoints and allowing We might consider scrapping the old HTTP API code. I believe it still works, but any client using it will be severely rate limited. Maybe phase it out after we're sure OAuth support is stable. |
oauth_session.go
Outdated
|
||
// NewLoginSession creates a new session for those who want to log into a | ||
// reddit account via OAuth. | ||
func NewOAuthSession(username, password, useragent, clientID, clientSecret string) (*OAuthSession, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't necessarily like passing more than a few args to functions (it's hard to remember the order without looking at documentation).
I was thinking maybe we could pass a struct or something instead.
However, I was also thinking we have two constructor functions for OAuthSession types: One for user/password tokens, and another for auth code tokens (asking for a user's permission).
edit: and currently useragent is not being used. I will fix that.
Sorry if I'm getting spammy. I will squash every thing neatly and rewrite the pull request body to explain the changes. In short: Most of the changes include API endpoint coverage - some "Me" endpoints, /api/submit support (for links and comments), and fetching subreddit listings. I added just enough testing to mock JSON responses, that's about it so far. I've also tinkered around with the constructor. o, err := NewOAuthSession("client_id", "client_secret", "Descriptive UserAgent v1.0", "http://my.url")
err = o.LoginAuth("my_bot_user", "my_bot_password")
// Now we can do API calls using variable o
^ I've changed this to using a library called Like I said, I'll organize everything better and do a proper write up (+docs) so that everything is crystal clear. Many of the changes come from personal taste and experimentation, so feel free to rip it all apart. I'm open to feedback always! |
8e852da
to
1baa3ad
Compare
After a new OAuthSession type is created with NewOAuthSession(), the caller is given the choice of how the auth token should be created. The auth token is required to define the HTTP client used for API requests. Currently, LoginAuth() is the only method of token exchange. Other methods should be implemented in the future, which is why LoginAuth() does not happen by default within the constructor of OAuthSession.
Disabled by default. Accepts a time.Duration and each HTTP request must wait on interval to elapse. User can disable any existing throttling by passing 0.
When creating a new OAuthSession type, the caller can now choose between authenticating for personal use or on behalf of a user.
Personal script authentication (password credential exchange) is not issued a refresh token by Reddit's API. The caller must request a new access token after each one expires. The TokenExpiry field indicates the lifetime of a token and will help the user determine when a new access token must be created.
* Adding sort to request for comments * Minor (automatic) changes by gofmt
This branch is rebased onto upstream/master. While I haven't used this branch in a while, I did fix the issue of receiving If anyone gets around to using this code, let me know of any issues... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks awesome! Just one minor nitpack about printing to stdout.
oauth_session.go
Outdated
o.throttle.Wait() | ||
} | ||
|
||
fmt.Println("Performing request...") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, if there's going to be any output, it should be to the standard logger and only if debug is enabled.
Hi @jzelinskie, Five things:
|
🎉 merged 🎉 Thanks again for all your effort! |
Thanks for your feedback! |
UPDATE
This patch set has changed quite a bit since May.
Here is a little how-to on the basic features of the OAuth code:
https://gist.github.com/aggrolite/160d5be23adb9e597553