This commit is contained in:
2026-02-21 18:37:15 +03:00
commit 408ae6a76b
7 changed files with 290 additions and 0 deletions

68
raindrop/auth.go Normal file
View File

@@ -0,0 +1,68 @@
package raindrop
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func (c *Client) OauthRedirect(w http.ResponseWriter, r *http.Request) {
authUrl, _ := url.Parse("https://raindrop.io/oauth/authorize")
params := url.Values{}
params.Set("redirect_uri", c.config.RedirectURI)
params.Set("client_id", c.config.ClientId)
params.Set("response_type", "code")
authUrl.RawQuery = params.Encode()
http.Redirect(w, r, authUrl.String(), http.StatusFound)
}
type TokensResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
// token lifetime in seconds
ExpiresIn int `json:"expires_in"`
}
func (c *Client) ExchangeOauthCode(code string) (*TokensResponse, error) {
body := struct {
GrantType string `json:"grant_type"`
Code string `json:"code"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURI string `json:"redirect_uri"`
}{
GrantType: "authorization_code",
Code: code,
ClientId: c.config.ClientId,
ClientSecret: c.config.ClientSecret,
RedirectURI: c.config.RedirectURI,
}
data, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to encode data to send in request: %v", err)
}
resp, err := http.Post("https://raindrop.io/oauth/access_token", "application/json", bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("failed to send request to raindrop api: %v", err)
}
defer resp.Body.Close()
response := &TokensResponse{}
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return nil, fmt.Errorf("failed to decode response from raindrop api: %v", err)
}
return response, nil
}
func (c *Client) SetApiToken(tokenType string, value string) {
c.token = &ApiToken{
Type: tokenType,
Value: value,
}
}