Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions api/access_token.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package api

import (
"strconv"
)

// AccessToken is an OAuth access token.
type AccessToken struct {
// The token value, typically a 40-character random string.
Token string
// Number of seconds until the access token expires.
ExpiresIn int
// The refresh token value, associated with the access token.
RefreshToken string
// Number of seconds until the refresh token expires.
RefreshTokenExpiresIn int
// The token type, e.g. "bearer".
Type string
// Space-separated list of OAuth scopes that this token grants.
Expand All @@ -15,11 +23,17 @@ type AccessToken struct {
// AccessToken extracts the access token information from a server response.
func (f FormResponse) AccessToken() (*AccessToken, error) {
if accessToken := f.Get("access_token"); accessToken != "" {
// Default to 0 if the expiry fields aren't present.
expiresIn, _ := strconv.Atoi(f.Get("expires_in"))
refreshTokenExpiresIn, _ := strconv.Atoi(f.Get("refresh_token_expires_in"))

return &AccessToken{
Token: accessToken,
RefreshToken: f.Get("refresh_token"),
Type: f.Get("token_type"),
Scope: f.Get("scope"),
Token: accessToken,
ExpiresIn: expiresIn,
RefreshToken: f.Get("refresh_token"),
RefreshTokenExpiresIn: refreshTokenExpiresIn,
Type: f.Get("token_type"),
Scope: f.Get("scope"),
}, nil
}

Expand Down