github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/google/drive/drive.go (about) 1 /* 2 Copyright 2013 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /* 18 Package drive registers the "googledrive" blobserver storage 19 type, storing blobs in a Google Drive folder. 20 21 Example low-level config: 22 23 "/storage-googledrive/": { 24 "handler": "storage-googledrive", 25 "handlerArgs": map[string]interface{}{ 26 "parent_id": parentId, 27 "auth": map[string]interface{}{ 28 "client_id": clientId, 29 "client_secret": clientSecret, 30 "refresh_token": refreshToken, 31 }, 32 }, 33 }, 34 */ 35 package drive 36 37 import ( 38 "net/http" 39 "time" 40 41 "camlistore.org/pkg/blobserver" 42 "camlistore.org/pkg/blobserver/google/drive/service" 43 "camlistore.org/pkg/jsonconfig" 44 "camlistore.org/third_party/code.google.com/p/goauth2/oauth" 45 ) 46 47 const ( 48 GoogleOAuth2AuthURL = "https://accounts.google.com/o/oauth2/auth" 49 GoogleOAuth2TokenURL = "https://accounts.google.com/o/oauth2/token" 50 ) 51 52 type driveStorage struct { 53 service *service.DriveService 54 } 55 56 func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) { 57 auth := config.RequiredObject("auth") 58 oauthConf := &oauth.Config{ 59 ClientId: auth.RequiredString("client_id"), 60 ClientSecret: auth.RequiredString("client_secret"), 61 AuthURL: GoogleOAuth2AuthURL, 62 TokenURL: GoogleOAuth2TokenURL, 63 } 64 65 // force refreshes the access token on start, make sure 66 // refresh request in parallel are being started 67 transport := &oauth.Transport{ 68 Token: &oauth.Token{ 69 AccessToken: "", 70 RefreshToken: auth.RequiredString("refresh_token"), 71 Expiry: time.Now(), 72 }, 73 Config: oauthConf, 74 Transport: http.DefaultTransport, 75 } 76 parent := config.RequiredString("parent_id") 77 if err := config.Validate(); err != nil { 78 return nil, err 79 } 80 service, err := service.New(transport, parent) 81 sto := &driveStorage{ 82 service: service, 83 } 84 return sto, err 85 } 86 87 func init() { 88 blobserver.RegisterStorageConstructor("googledrive", blobserver.StorageConstructor(newFromConfig)) 89 }