Skip to content

Commit

Permalink
fix refresh token read
Browse files Browse the repository at this point in the history
  • Loading branch information
rokerzfirst101 committed Jan 11, 2024
1 parent 8754e4a commit 624375c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
8 changes: 4 additions & 4 deletions configs/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ APP_NAME='Event Migration Script'
APP_VERSION=v0.0.1
HTTP_PORT=8001

GOOGLE_CREDENTIALS=
REFRESH_TOKEN=
GOOGLE_CREDENTIALS={"web":{"client_id":"15139215229-7olau25gs9jn1fuig1cgu8fpnrqegggr.apps.googleusercontent.com","project_id":"hiring-motion-test","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost:8000/callback","https://developers.google.com/oauthplayground","https://oauth.pstmn.io/v1/callback","http://localhost:8001/api/auth/callback/google"],"javascript_origins":["http://localhost","http://localhost:3000","http://localhost:8001","http://localhost:3001","http://localhost:3002"]}}
REFRESH_TOKEN=1//0440E8nsNXUl1CgYIARAAGAQSNwF-L9IrZVvkqWXMitsXeLrguGpEvMzdvUW9VQMAqJvifmjCo3ZjbgBRyVhRcNe7Knx6Nvr4yYg

SOURCE_CALENDAR=
DESTINATION_CALENDAR=
SOURCE_CALENDAR=0e3a78f0ce9c0b300b2f60f1e1aa7ca593411a5c3ad9078cc07a07add50804f3@group.calendar.google.com
DESTINATION_CALENDAR=c_5d1a220005718977e00c4fca7944ab09395271da10bd3cfe9d2cbc6046c9dcfa@group.calendar.google.com

EVENT_TIME_MIN=
EVENT_TIME_MAX=
Expand Down
24 changes: 8 additions & 16 deletions google/token/token.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package token

import (
"encoding/json"
"fmt"
"net/http"

Expand All @@ -11,44 +10,37 @@ import (
"golang.org/x/oauth2/google"
)

const (
GoogleCalendar = "calendar"
)

func NewClientFromEnv(ctx *gofr.Context, credentialEnvKey, tokenEnvKey string, scopes []string) (*http.Client, error) {
credentialData, err := readEnvValue(ctx, credentialEnvKey)
if err != nil {
return nil, err
}

tokenData, err := readEnvValue(ctx, tokenEnvKey)
if err != nil {
return nil, err
}

var token *oauth2.Token
credentialBytes := []byte(credentialData)

err = json.Unmarshal(tokenData, &token)
refreshToken, err := readEnvValue(ctx, tokenEnvKey)
if err != nil {
return nil, err
}

config, err := google.ConfigFromJSON(credentialData, scopes...)
token := &oauth2.Token{RefreshToken: refreshToken}

config, err := google.ConfigFromJSON(credentialBytes, scopes...)
if err != nil {
return nil, err
}

return config.Client(ctx, token), nil
}

func readEnvValue(ctx *gofr.Context, envKey string) ([]byte, error) {
func readEnvValue(ctx *gofr.Context, envKey string) (string, error) {
value := ctx.Config.Get(envKey)
if value == "" {
return nil, &gofrErr.Response{
return "", &gofrErr.Response{
StatusCode: http.StatusInternalServerError,
Reason: fmt.Sprintf("Unable to read %s from environment", envKey),
}
}

return []byte(value), nil
return value, nil
}
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ The script is setup to migrate events from first to last in chronological order.
| BACKOFF_TIME | Time to wait between retries. | No |

> Note: The `EVENT_TIME_MIN` and `EVENT_TIME_MAX` are in RFC3339 format. E.g.: `2023-08-10T21:27:37+05:30`
> Example Refresh Token Value: "{"refresh_token": "{refreshToken}"}"

0 comments on commit 624375c

Please sign in to comment.