Google Picker API已保存用户名和密码

时间:2015-03-20 10:29:29

标签: javascript asp.net google-api google-picker

我已实施Google Picker API,会弹出一个窗口,询问Google authentication,然后向我们展示Google云端硬盘中的所有可用文件。 一切正常。

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

有没有办法传递用户凭据并获得授权并访问文件?

我的代码:

<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>

2 个答案:

答案 0 :(得分:0)

创建选择器时,即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.');
}
}

答案 1 :(得分: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.');
}
}
相关问题