Ejemplo n.º 1
0
// getOAuthClient returns an oauth client (either from cached credentials or
// via an authentication flow) or nil depending on whether doOauth is false.
func getOAuthClient(doOauth bool, cacheFilePath string) *http.Client {
	if doOauth {
		config := auth.DefaultOAuthConfig(cacheFilePath)
		client, err := auth.RunFlow(config)
		if err != nil {
			glog.Fatalf("Failed to auth: %s", err)
		}
		return client
	}
	return nil
}
Ejemplo n.º 2
0
// getClient returns an authorized storage.Service and the
// corresponding http.Client; if anything goes wrong, it logs a fatal
// error.
func getClient() (storageClient, error) {
	var client *http.Client
	var err error
	if *local {
		client, err = auth.RunFlow(auth.OAuthConfig(*oauthCacheFile, auth.SCOPE_FULL_CONTROL))
		// TODO(stephana): Replace auth.RunFlow with auth.NewClient
		// client, err = auth.NewClient(true, *oauthCacheFile, auth.SCOPE_FULL_CONTROL, auth.SCOPE_GCE)
	} else {
		client = auth.GCEServiceAccountClient(&http.Transport{Dial: util.DialTimeout})
	}
	if err != nil {
		return storageClient{}, err
	}
	gsService, err := storage.New(client)
	if err != nil {
		return storageClient{}, err
	}
	return storageClient{httpClient: client, storageService: gsService}, nil
}
Ejemplo n.º 3
0
// NewGMail returns a new GMail object which is authorized to send email.
func NewGMail(clientId, clientSecret, tokenCacheFile string) (*GMail, error) {
	config := oauth.Config{
		ClientId:     clientId,
		ClientSecret: clientSecret,
		Scope:        gmail.GmailComposeScope,
		AuthURL:      "https://accounts.google.com/o/oauth2/auth",
		TokenURL:     "https://accounts.google.com/o/oauth2/token",
		TokenCache:   oauth.CacheFile(tokenCacheFile),
		RedirectURL:  "urn:ietf:wg:oauth:2.0:oob",
		AccessType:   "offline",
	}
	client, err := auth.RunFlow(&config)
	if err != nil {
		return nil, err
	}
	service, err := gmail.New(client)
	if err != nil {
		return nil, err
	}
	return &GMail{
		service: service,
	}, nil
}