2

我已经实现Google Picker API了它会弹出一个窗口询问Google authentication,然后向我们展示 Google Drive 中所有可用的文件。一切正常。

但现在我的要求是,我需要访问驱动器中的文件,而无需弹出身份验证。我将用户名和密码保存在我的数据库中。

有什么方法可以传递用户凭据并获得授权和访问文件?

我的代码:

<script>
  function onApiLoad() {
    gapi.load('auth', { 'callback': onAuthApiLoad });
    gapi.load('picker');
  }

function onAuthApiLoad() {
   window.gapi.auth.authorize({                         
   'client_id': 'xxxx.apps.googleusercontent.com',
   'scope': ['https://www.googleapis.com/auth/drive']
    }, handleAuthResult);
}

var oauthToken;
function handleAuthResult(authResult) {
   if (authResult && !authResult.error) {
      oauthToken = authResult.access_token;
      createPicker();
   }
}


function createPicker() {                    
   var picker = new google.picker.PickerBuilder()
                .addView(new google.picker.DocsUploadView())
                .addView(new google.picker.DocsView())                        
                .setOAuthToken(oauthToken)
                .setDeveloperKey('xxxx')                           
                .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
                .addView(new google.picker.VideoSearchView().setSite(google.picker.VideoSearchView.YOUTUBE))
                .setCallback(pickerCallback)
                .build();
                 picker.setVisible(true);
      }
</script>
4

2 回答 2

1

在创建选择器时,即 createpicker() 发送访问令牌,以便它不会提示同意屏幕。在您的代码中将 createpicker() 更改为 createPicker(oauthToken)。请找到示例代码片段:

function createPicker(token) {
 if (pickerApiLoaded && token) {


 var picker = new google.picker.PickerBuilder()
       .addView(new google.picker.DocsView().setIncludeFolders(true)) //All files and Folders            

     .setOAuthToken(token)//Access Token

     .setDeveloperKey(DEVELOPER_KEY)
     .setCallback(pickerCallback)
     // Instruct Picker to fill the dialog, minus 2 pixels for the border.
     .setSize(DIALOG_DIMENSIONS.width ,
         DIALOG_DIMENSIONS.height )
     .build();
 picker.setVisible(true);
} else {
 showError('Unable to load the file picker.');
}
}
于 2015-03-20T17:53:17.267 回答
0

通过传递您的 access_token 尝试以下代码..

    function createPicker(access_token) {
 if (access_token) {
  var picker = new google.picker.PickerBuilder()
            .addView(new google.picker.DocsUploadView())
            .addView(new google.picker.DocsView())                        
            .setOAuthToken(access_token)
            .setDeveloperKey('xxxx')                           
            .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
            .addView(new google.picker.VideoSearchView().setSite(google.picker.VideoSearchView.YOUTUBE))
            .setCallback(pickerCallback)
            .build();
             picker.setVisible(true);
} else {
 showError('Unable to load the file picker.');
}
}
于 2016-06-24T18:28:18.813 回答