Skip to content
This repository has been archived by the owner on Apr 19, 2021. It is now read-only.

Commit

Permalink
feat: refresh access token
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergei Kuvakin committed Feb 8, 2021
1 parent e37c562 commit b645a1b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions clubhouseapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ func GetChannels() (GetChannelsResponse, error) {
return data, nil
}

// RefreshToken is a method for getting a new access token
func RefreshToken(refreshToken string) (RefreshTokenResponse, error) {
var data RefreshTokenResponse
response, err := http.Post(fmt.Sprintf("%s/refresh_token", url), strings.NewReader(fmt.Sprintf(`{"refresh":"%s"}`, refreshToken)), headers)
if err != nil {
return data, err
}
fmt.Println(string(response))
if err := json.Unmarshal(response, &data); err != nil {
return data, err
}
return data, nil
}

// AddCredentials is a method of adding credentials such as User ID or Access Token
func AddCredentials(credentials map[string]string) {
for k, v := range credentials {
Expand Down
13 changes: 13 additions & 0 deletions example/get_channels.go → example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,26 @@ func channels(userID, accessToken string) {
fmt.Println(response)
}

func refresh(refreshToken string) {
response, err := clubhouseapi.RefreshToken(refreshToken)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("AccessToken", response.Access)
fmt.Println("RefreshToken", response.Refresh)
}

func main() {
_ = godotenv.Load()
// phoneNumber := os.Getenv("PHONE_NUMBER")
// login(phoneNumber)
// verificationCode := "1234"
// auth(phoneNumber, verificationCode)

// refreshToken := os.Getenv("REFRESH_TOKEN")
// refresh(refreshToken)

userID := os.Getenv("USER_ID")
accessToken := os.Getenv("ACCESS_TOKEN")
channels(userID, accessToken)
Expand Down
6 changes: 6 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,9 @@ type GetChannelsResponse struct {
} `json:"events"`
Success bool `json:"success"`
}

// RefreshTokenResponse is the response structure of the RefreshToken method
type RefreshTokenResponse struct {
Access string `json:"access"`
Refresh string `json:"refresh"`
}

0 comments on commit b645a1b

Please sign in to comment.