Skip to content

Commit

Permalink
allows specifying request token via GET parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Mar 7, 2020
1 parent 96a185c commit 33eb8eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 5 additions & 11 deletions pkg/common.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package pkg

import (
"errors"
"net/http"
"context"
"time"

"github.com/kelseyhightower/envconfig"
Expand Down Expand Up @@ -51,20 +50,15 @@ func init() {
oauthConfig.ClientSecret = cfg.GoogleClientSecret
}

func ytServiceViaBasicAuth(r *http.Request) (*youtube.Service, error) {
_, password, ok := r.BasicAuth()
if !ok || password == "" {
return nil, errors.New("please provide refresh token")
}

func ytServiceFromRefreshToken(ctx context.Context, refreshToken string) (*youtube.Service, error) {
oauthToken := &oauth2.Token{
Expiry: time.Now(),
TokenType: "Bearer",
RefreshToken: password,
RefreshToken: refreshToken,
}

return youtube.NewService(
r.Context(),
option.WithTokenSource(oauthConfig.TokenSource(r.Context(), oauthToken)),
ctx,
option.WithTokenSource(oauthConfig.TokenSource(ctx, oauthToken)),
)
}
8 changes: 7 additions & 1 deletion pkg/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ const (
)

func ExportHandler(w http.ResponseWriter, r *http.Request) {
yt, err := ytServiceViaBasicAuth(r)
refreshToken := r.URL.Query().Get("refreshToken")
if refreshToken == "" {
http.Error(w, "please specify refresh token", http.StatusBadRequest)
return
}

yt, err := ytServiceFromRefreshToken(r.Context(), refreshToken)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
Expand Down

0 comments on commit 33eb8eb

Please sign in to comment.