Created
December 5, 2015 13:56
-
-
Save deltam/41f3554ec70f550deac0 to your computer and use it in GitHub Desktop.
Google Drive APIでアップロードしたファイルの公開範囲を変更する ref: http://qiita.com/deltam/items/e13479ef36ac6729063e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"code.google.com/p/goauth2/oauth" | |
"fmt" | |
"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) | |
// 公開範囲変更のために追加したコード | |
pm := &drive.Permission{ | |
Type: "domain", | |
Role: "reader", // 閲覧者設定 | |
Value: "{GoogleAppsのドメイン}", | |
} | |
// SendNotificationEmails(false)にしないとスクリプト実行のたびにメールが飛んでしまう(? | |
rpm, err := svc.Permissions.Insert(r.Id, pm).SendNotificationEmails(false).Do() | |
if err != nil { | |
log.Fatalf("Permission error : %v\n", err) | |
} | |
log.Printf("Permission: type:%v, role:%v\n", rpm.Type, rpm.Role) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment