Skip to content

Commit

Permalink
feat(utils/google_drive_utils): Add GetToken method
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Oct 23, 2023
1 parent c1c93c9 commit 4c8530e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions utils/google_drive_utils/google_drive_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"time"

"golang.org/x/oauth2"
drive "google.golang.org/api/drive/v3"
Expand Down Expand Up @@ -51,3 +52,34 @@ func Upload(clientId, clientSecret, accessToken, fileToUpload string) {

fmt.Printf("File ID: %s\n", file.Id)
}

func GetToken(clientId, clientSecret string) {
ctx := context.Background()
conf := &oauth2.Config{
ClientID: clientId,
ClientSecret: clientSecret,
Scopes: []string{"https://www.googleapis.com/auth/drive.file"},
Endpoint: oauth2.Endpoint{
TokenURL: "https://oauth2.googleapis.com/token",
DeviceAuthURL: "https://oauth2.googleapis.com/device/code",
},
}

code, err := conf.DeviceAuth(ctx)
if err != nil {
log.Fatalf("Failed to get device and user codes: %v", err)
}

fmt.Printf("Visit the URL: %s\n", code.VerificationURI)
fmt.Printf("And enter the code: %s\n", code.UserCode)

for {
token, err := conf.DeviceAccessToken(ctx, code)
if err == nil {
fmt.Printf("Got access token: %s\n", token.AccessToken)
fmt.Printf("Got refresh token: %s\n", token.RefreshToken)
break
}
time.Sleep(1 * time.Second)
}
}

0 comments on commit 4c8530e

Please sign in to comment.