From 9f0c9c1f2e11806b9a49121662b8e954a368292b Mon Sep 17 00:00:00 2001 From: Priyankarp24 Date: Wed, 6 Apr 2022 14:18:21 +0530 Subject: [PATCH 1/2] Updated classroom snippets using default authentication --- classroom/snippets/createCourse.go | 18 ++++++++- classroom/snippets/getCourse.go | 20 +++++----- classroom/snippets/oauth.go | 64 ------------------------------ 3 files changed, 25 insertions(+), 77 deletions(-) delete mode 100644 classroom/snippets/oauth.go diff --git a/classroom/snippets/createCourse.go b/classroom/snippets/createCourse.go index f98b651..dcd37c3 100644 --- a/classroom/snippets/createCourse.go +++ b/classroom/snippets/createCourse.go @@ -19,6 +19,7 @@ package main import ( "context" "fmt" + "golang.org/x/oauth2/google" "log" "net/http" @@ -39,13 +40,26 @@ func createCourse(client *http.Client) { DescriptionHeading: "Welcome to 10th Grade Biology", Description: "We'll be learning about about the structure of living creatures from a combination of textbooks, guest lectures, and lab work. Expect to be excited!", Room: "301", - OwnerId: "me", + OwnerId: "savat@google.com", CourseState: "PROVISIONED", } course, err := srv.Courses.Create(c).Do() if err != nil { log.Fatalf("Course unable to be created %v", err) } - // [END classroom_create_course] fmt.Printf("Created course: %v", course.Id) + // [END classroom_create_course] +} +func main() { + ctx := context.Background() + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity and + https://cloud.google.com/docs/authentication/production for + guides on implementing OAuth2 for your application. + */ + client, err := google.DefaultClient(ctx, classroom.ClassroomCoursesScope) + if err != nil { + log.Fatalf("Failed Default authentication: %v", err) + } + createCourse(client) } diff --git a/classroom/snippets/getCourse.go b/classroom/snippets/getCourse.go index a929edd..cf48b57 100644 --- a/classroom/snippets/getCourse.go +++ b/classroom/snippets/getCourse.go @@ -19,7 +19,6 @@ package main import ( "context" "fmt" - "io/ioutil" "log" "net/http" @@ -40,21 +39,20 @@ func getCourse(client *http.Client) { if err != nil { log.Fatalf("Course unable to be retrieved %v", err) } - // [END classroom_get_course] fmt.Printf("Course with ID %v found.", course.Id) + // [END classroom_get_course] } func main() { - b, err := ioutil.ReadFile("credentials.json") - if err != nil { - log.Fatalf("Unable to read client secret file: %v", err) - } - - // If modifying these scopes, delete your previously saved token.json. - config, err := google.ConfigFromJSON(b, classroom.ClassroomCoursesScope) + ctx := context.Background() + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity and + https://cloud.google.com/docs/authentication/production for + guides on implementing OAuth2 for your application. + */ + client, err := google.DefaultClient(ctx, classroom.ClassroomCoursesScope) if err != nil { - log.Fatalf("Unable to parse client secret file to config: %v", err) + log.Fatalf("Failed Default authentication: %v", err) } - client := getClient(config) getCourse(client) } diff --git a/classroom/snippets/oauth.go b/classroom/snippets/oauth.go deleted file mode 100644 index e3eeb47..0000000 --- a/classroom/snippets/oauth.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "log" - "net/http" - "os" - - "golang.org/x/oauth2" -) - -// Retrieve a token, saves the token, then returns the generated client. -func getClient(config *oauth2.Config) *http.Client { - tokFile := "token.json" - tok, err := tokenFromFile(tokFile) - if err != nil { - tok = getTokenFromWeb(config) - saveToken(tokFile, tok) - } - return config.Client(context.Background(), tok) -} - -// Request a token from the web, then returns the retrieved token. -func getTokenFromWeb(config *oauth2.Config) *oauth2.Token { - authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline) - fmt.Printf("Go to the following link in your browser then type the "+ - "authorization code: \n%v\n", authURL) - - var authCode string - if _, err := fmt.Scan(&authCode); err != nil { - log.Fatalf("Unable to read authorization code: %v", err) - } - - tok, err := config.Exchange(context.TODO(), authCode) - if err != nil { - log.Fatalf("Unable to retrieve token from web: %v", err) - } - return tok -} - -// Retrieves a token from a local file. -func tokenFromFile(file string) (*oauth2.Token, error) { - f, err := os.Open(file) - if err != nil { - return nil, err - } - defer f.Close() - tok := &oauth2.Token{} - err = json.NewDecoder(f).Decode(tok) - return tok, err -} - -// Saves a token to a file path. -func saveToken(path string, token *oauth2.Token) { - fmt.Printf("Saving credential file to: %s\n", path) - f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - log.Fatalf("Unable to cache oauth token: %v", err) - } - defer f.Close() - json.NewEncoder(f).Encode(token) -} From 8f72a778bd66d8f6bed97ad03e395717abc5ea5f Mon Sep 17 00:00:00 2001 From: Priyankarp24 Date: Mon, 11 Apr 2022 15:31:06 +0530 Subject: [PATCH 2/2] Update createCourse.go --- classroom/snippets/createCourse.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classroom/snippets/createCourse.go b/classroom/snippets/createCourse.go index dcd37c3..6b0e20f 100644 --- a/classroom/snippets/createCourse.go +++ b/classroom/snippets/createCourse.go @@ -40,7 +40,7 @@ func createCourse(client *http.Client) { DescriptionHeading: "Welcome to 10th Grade Biology", Description: "We'll be learning about about the structure of living creatures from a combination of textbooks, guest lectures, and lab work. Expect to be excited!", Room: "301", - OwnerId: "savat@google.com", + OwnerId: "me", CourseState: "PROVISIONED", } course, err := srv.Courses.Create(c).Do()