Skip to content

Instantly share code, notes, and snippets.

@deltam
Last active December 5, 2015 13:55
Show Gist options
  • Save deltam/6ffb99238ba2af5ae698 to your computer and use it in GitHub Desktop.
Save deltam/6ffb99238ba2af5ae698 to your computer and use it in GitHub Desktop.
CSVダウンロードを作るのに飽きたからGoogle Drive APIを使ってみた ref: http://qiita.com/deltam/items/3a806bd9f58090bfe677
package main
import (
"bytes" // 追加
"code.google.com/p/goauth2/oauth"
"fmt"
// "code.google.com/p/google-api-go-client/drive/v2"
"google.golang.org/api/drive/v2" // こっちに書き換え
"log"
"net/http"
)
// Settings for authorization.
var config = &oauth.Config{
ClientId: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET",
Scope: "https://www.googleapis.com/auth/drive",
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
TokenCache: oauth.CacheFile("cache.json"), // キャッシュするファイル名を指定
}
// Uploads a file to Google Drive
func main() {
t := &oauth.Transport{
Config: config,
Transport: http.DefaultTransport,
}
// キャッシュがあったらブラウザ認証しない
_, err := config.TokenCache.Token()
if err != nil {
// Generate a URL to visit for authorization.
authUrl := config.AuthCodeURL("state")
log.Printf("Go to the following link in your browser: %v\n", authUrl)
// Read the code, and exchange it for a token.
log.Printf("Enter verification code: ")
var code string
fmt.Scanln(&code)
_, err := t.Exchange(code)
if err != nil {
log.Fatalf("An error occurred exchanging the code: %v\n", err)
}
}
// Create a new authorized Drive client.
svc, err := drive.New(t.Client())
if err != nil {
log.Fatalf("An error occurred creating Drive client: %v\n", err)
}
// Define the metadata for the file we are going to create.
f := &drive.File{
Title: "My Document",
Description: "My test document",
MimeType: "text/csv", // 追加
}
// CSV文字列
nameList := `no, name
1, アブドゥル
2, イギー
3, 花京院
`
csvReader := bytes.NewReader([]byte(nameList))
// Make the API request to upload metadata and file data.
r, err := svc.Files.Insert(f).Media(csvReader).Do()
if err != nil {
log.Fatalf("An error occurred uploading the document: %v\n", err)
}
log.Printf("Created: ID=%v, Title=%v\n", r.Id, r.Title)
}
package main
import (
"code.google.com/p/goauth2/oauth"
"fmt"
// "code.google.com/p/google-api-go-client/drive/v2"
"google.golang.org/api/drive/v2" // こっちに書き換え
"log"
"net/http"
"os"
)
// Settings for authorization.
var config = &oauth.Config{
ClientId: "YOUR_CLIENT_ID", // ここは作った認証情報のIDに書き換える
ClientSecret: "YOUR_CLIENT_SECRET", // ここもSecretに書き換える
Scope: "https://www.googleapis.com/auth/drive",
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
// Uploads a file to Google Drive
func main() {
// Generate a URL to visit for authorization.
authUrl := config.AuthCodeURL("state")
log.Printf("Go to the following link in your browser: %v\n", authUrl)
t := &oauth.Transport{
Config: config,
Transport: http.DefaultTransport,
}
// Read the code, and exchange it for a token.
log.Printf("Enter verification code: ")
var code string
fmt.Scanln(&code)
_, err := t.Exchange(code)
if err != nil {
log.Fatalf("An error occurred exchanging the code: %v\n", err)
}
// Create a new authorized Drive client.
svc, err := drive.New(t.Client())
if err != nil {
log.Fatalf("An error occurred creating Drive client: %v\n", err)
}
// Define the metadata for the file we are going to create.
f := &drive.File{
Title: "My Document",
Description: "My test document",
}
// Read the file data that we are going to upload.
m, err := os.Open("document.txt")
if err != nil {
log.Fatalf("An error occurred reading the document: %v\n", err)
}
// Make the API request to upload metadata and file data.
r, err := svc.Files.Insert(f).Media(m).Do()
if err != nil {
log.Fatalf("An error occurred uploading the document: %v\n", err)
}
log.Printf("Created: ID=%v, Title=%v\n", r.Id, r.Title)
}
go get google.golang.org/api/drive/v2
go get golang.org/x/oauth2/...
# サンプルではこちらを使う
go get code.google.com/p/goauth2
package main
import (
"code.google.com/p/goauth2/oauth"
"fmt"
// "code.google.com/p/google-api-go-client/drive/v2"
"google.golang.org/api/drive/v2" // こっちに書き換え
"log"
"net/http"
"os"
)
// Settings for authorization.
var config = &oauth.Config{
ClientId: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET",
Scope: "https://www.googleapis.com/auth/drive",
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
TokenCache: oauth.CacheFile("cache.json"), // キャッシュするファイル名を指定
}
// Uploads a file to Google Drive
func main() {
t := &oauth.Transport{
Config: config,
Transport: http.DefaultTransport,
}
// キャッシュがあったらブラウザ認証しない
_, err := config.TokenCache.Token()
if err != nil {
// Generate a URL to visit for authorization.
authUrl := config.AuthCodeURL("state")
log.Printf("Go to the following link in your browser: %v\n", authUrl)
// Read the code, and exchange it for a token.
log.Printf("Enter verification code: ")
var code string
fmt.Scanln(&code)
_, err := t.Exchange(code)
if err != nil {
log.Fatalf("An error occurred exchanging the code: %v\n", err)
}
}
// Create a new authorized Drive client.
svc, err := drive.New(t.Client())
if err != nil {
log.Fatalf("An error occurred creating Drive client: %v\n", err)
}
// Define the metadata for the file we are going to create.
f := &drive.File{
Title: "My Document",
Description: "My test document",
}
// Read the file data that we are going to upload.
m, err := os.Open("document.txt")
if err != nil {
log.Fatalf("An error occurred reading the document: %v\n", err)
}
// Make the API request to upload metadata and file data.
r, err := svc.Files.Insert(f).Media(m).Do()
if err != nil {
log.Fatalf("An error occurred uploading the document: %v\n", err)
}
log.Printf("Created: ID=%v, Title=%v\n", r.Id, r.Title)
}
$ echo "hello gdrive" > document.txt
$ go run gdrive_sample.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment