{"id":19651740,"url":"https://github.com/tanaikech/getcode","last_synced_at":"2026-01-19T14:32:04.888Z","repository":{"id":57551102,"uuid":"94964838","full_name":"tanaikech/getcode","owner":"tanaikech","description":"This is a Golang library to automatically get an authorization code for retrieving access token using OAuth2.","archived":false,"fork":false,"pushed_at":"2022-02-26T06:29:23.000Z","size":8,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-09T22:43:42.704Z","etag":null,"topics":["authoring-tool","go-lib","go-library","golang","oauth2"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tanaikech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-21T05:06:15.000Z","updated_at":"2022-08-21T03:11:44.000Z","dependencies_parsed_at":"2022-09-26T18:41:20.392Z","dependency_job_id":null,"html_url":"https://github.com/tanaikech/getcode","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanaikech%2Fgetcode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanaikech%2Fgetcode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanaikech%2Fgetcode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanaikech%2Fgetcode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tanaikech","download_url":"https://codeload.github.com/tanaikech/getcode/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247182418,"owners_count":20897381,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authoring-tool","go-lib","go-library","golang","oauth2"],"created_at":"2024-11-11T15:07:46.510Z","updated_at":"2026-01-19T14:32:04.882Z","avatar_url":"https://github.com/tanaikech.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# getcode\r\n\r\n\u003ca name=\"top\"\u003e\u003c/a\u003e\r\n\r\n# Overview\r\n\r\nThis is a Golang library to automatically get an authorization code for retrieving access token using OAuth2.\r\n\r\n# Description\r\n\r\nWhen it retrieves an access token and refresh token using OAuth2, the code for retrieving them has to be got by authorization on own browser. In order to retrieve the code, in generally, users have to click the authorization button and copy the code on the browser. This library can be automatically got the code by launching HTML server as a redirected server. At first, I have used this for retrieving the code from Google. But recently I noticed that this can be used for other sites. They are Google, GitHub, Slack and so on. This library can be used for creating such applications.\r\n\r\nThis method was used for [gogauth](https://github.com/tanaikech/gogauth). Next, it was used for [ggsrun](https://github.com/tanaikech/ggsrun) And, this was recreated as a library.\r\n\r\n# Install\r\n\r\nYou can get this by\r\n\r\n```bash\r\n$ go get -u github.com/tanaikech/getcode\r\n```\r\n\r\n- If \"go get\" cannot be used, please use this library with `GO111MODULE=on`. And `import(\"github.com/tanaikech/getcode\")`\r\n\r\n# Usage\r\n\r\n```\r\ncode := getcode.Init(CodeURL, ServerPort, waitingTime, showing, manual).Do()\r\n```\r\n\r\n- CodeURL (string) : URL to get code\r\n- ServerPort (int) : Server port\r\n- waitingTime(second) (int) : waiting time for request\r\n- showing (boolean) : Display message for authorization\r\n- manual (boolean) : If you use manual mode, it's true.\r\n\r\n- In this case, please set `http://localhost:8080` as the redirect URI.\r\n\r\n## Flow\r\n\r\n1. `getcode` is run, your browser is launched and waits for login to the site you want to retrieve access token.\r\n1. After logged in the site, you will see the button for authorizing. When push it, the HTML server by this library retrieves the authorization code.\r\n1. The authorization code can be retrieved automatically. And `Done`. is displayed on your terminal.\r\n\r\nIf your browser isn't launched or spends for 30 seconds from the wait of authorization, it becomes the input work queue. This is a manual mode. Please copy displayed URL and paste it to your browser, and login the site.\r\n\r\n**By above flow, the authorization code can be retrieved.**\r\n\r\n# Samples\r\n\r\nThese are the samples for retrieving the code from Google, GitHub and Slack.\r\n\r\n### 1. In the case of Google\r\n\r\n```go\r\ncode := getcode.Init(CodeURL, 8080, 30, true, false).Do()\r\n```\r\n\r\n- CodeURL : `https://accounts.google.com/o/oauth2/auth?client_id=###\u0026redirect_uri=http://localhost:8080\u0026scope=###\u0026response_type=code\u0026approval_prompt=force\u0026access_type=offline`\r\n- ServerPort : 8080\r\n- waitingTime : 30\r\n- showing message : true\r\n- manual : false\r\n\r\nIn this case, please create the OAuth2 credential as Web application. please set `http://localhost:8080` as the redirect URI.\r\n\r\nAs a sample script, when this library is used for [the sample script of Quickstart for Go of Drive API](https://developers.google.com/drive/api/v3/quickstart/go), it becomes as follows. When you run this script, a browser is automatically opened and when you authorize the scopes, the authorization code is automatically retrieved, and `token.json` is created.\r\n\r\n```go\r\npackage main\r\n\r\nimport (\r\n\t\"context\"\r\n\t\"encoding/json\"\r\n\t\"fmt\"\r\n\t\"io/ioutil\"\r\n\t\"log\"\r\n\t\"net/http\"\r\n\t\"os\"\r\n\r\n\t\"golang.org/x/oauth2\"\r\n\t\"golang.org/x/oauth2/google\"\r\n\t\"google.golang.org/api/drive/v3\"\r\n\t\"google.golang.org/api/option\"\r\n\r\n\t\"github.com/tanaikech/getcode\"\r\n)\r\n\r\n// Retrieve a token, saves the token, then returns the generated client.\r\nfunc getClient(config *oauth2.Config) *http.Client {\r\n\t// The file token.json stores the user's access and refresh tokens, and is\r\n\t// created automatically when the authorization flow completes for the first\r\n\t// time.\r\n\ttokFile := \"token.json\"\r\n\ttok, err := tokenFromFile(tokFile)\r\n\tif err != nil {\r\n\t\ttok = getTokenFromWeb(config)\r\n\t\tsaveToken(tokFile, tok)\r\n\t}\r\n\treturn config.Client(context.Background(), tok)\r\n}\r\n\r\n// Request a token from the web, then returns the retrieved token.\r\nfunc getTokenFromWeb(config *oauth2.Config) *oauth2.Token {\r\n\tauthURL := config.AuthCodeURL(\"state-token\", oauth2.AccessTypeOffline)\r\n\r\n\t// Here, this library is used.\r\n    // In this case, the redirect URI is http://localhost:8080\r\n\tcode := getcode.Init(authURL, 8080, 30, true, false).Do()\r\n\r\n\ttok, err := config.Exchange(context.TODO(), code)\r\n\tif err != nil {\r\n\t\tlog.Fatalf(\"Unable to retrieve token from web %v\", err)\r\n\t}\r\n\treturn tok\r\n}\r\n\r\n// Retrieves a token from a local file.\r\nfunc tokenFromFile(file string) (*oauth2.Token, error) {\r\n\tf, err := os.Open(file)\r\n\tif err != nil {\r\n\t\treturn nil, err\r\n\t}\r\n\tdefer f.Close()\r\n\ttok := \u0026oauth2.Token{}\r\n\terr = json.NewDecoder(f).Decode(tok)\r\n\treturn tok, err\r\n}\r\n\r\n// Saves a token to a file path.\r\nfunc saveToken(path string, token *oauth2.Token) {\r\n\tfmt.Printf(\"Saving credential file to: %s\\n\", path)\r\n\tf, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)\r\n\tif err != nil {\r\n\t\tlog.Fatalf(\"Unable to cache oauth token: %v\", err)\r\n\t}\r\n\tdefer f.Close()\r\n\tjson.NewEncoder(f).Encode(token)\r\n}\r\n\r\nfunc main() {\r\n\tctx := context.Background()\r\n\tb, err := ioutil.ReadFile(\"credentials.json\")\r\n\tif err != nil {\r\n\t\tlog.Fatalf(\"Unable to read client secret file: %v\", err)\r\n\t}\r\n\r\n\t// If modifying these scopes, delete your previously saved token.json.\r\n\tconfig, err := google.ConfigFromJSON(b, drive.DriveMetadataReadonlyScope)\r\n\tif err != nil {\r\n\t\tlog.Fatalf(\"Unable to parse client secret file to config: %v\", err)\r\n\t}\r\n\tclient := getClient(config)\r\n\r\n\tsrv, err := drive.NewService(ctx, option.WithHTTPClient(client))\r\n\tif err != nil {\r\n\t\tlog.Fatalf(\"Unable to retrieve Drive client: %v\", err)\r\n\t}\r\n\r\n\tr, err := srv.Files.List().PageSize(10).\r\n\t\tFields(\"nextPageToken, files(id, name)\").Do()\r\n\tif err != nil {\r\n\t\tlog.Fatalf(\"Unable to retrieve files: %v\", err)\r\n\t}\r\n\tfmt.Println(\"Files:\")\r\n\tif len(r.Files) == 0 {\r\n\t\tfmt.Println(\"No files found.\")\r\n\t} else {\r\n\t\tfor _, i := range r.Files {\r\n\t\t\tfmt.Printf(\"%s (%s)\\n\", i.Name, i.Id)\r\n\t\t}\r\n\t}\r\n}\r\n```\r\n\r\n### 2. In the case of GitHub\r\n\r\n- CodeURL : `https://github.com/login/oauth/authorize?client_id=###\u0026scope=###`\r\n- ServerPort : 8080\r\n- waitingTime : 30\r\n- showing message : true\r\n- manual : false\r\n\r\nIn this case, please set `http://localhost:8080` as the redirect URI.\r\n\r\n### 3. In the case of Slack\r\n\r\n- CodeURL : `https://slack.com/oauth/authorize?client_id=###\u0026scope=###`\r\n- ServerPort : 8080\r\n- waitingTime : 30\r\n- showing message : true\r\n- manual : false\r\n\r\nIn this case, please set `http://localhost:8080` as the redirect URI.\r\n\r\n\u003ca name=\"Update_History\"\u003e\u003c/a\u003e\r\n\r\n# Update History\r\n\r\n- v1.0.0 (June 21, 2017)\r\n\r\n  Initial release.\r\n\r\n- v1.0.1 (February 26, 2022)\r\n\r\n  Latest libraries are reflected to this library. And, the sample script is shown using the Quickstart for go of Drive API.\r\n\r\n[TOP](#top)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftanaikech%2Fgetcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftanaikech%2Fgetcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftanaikech%2Fgetcode/lists"}