以下 javascript 代码使用 web 应用程序帐户访问驱动器 api。我需要一些帮助来修改代码,以便它可以使用整个应用程序的服务帐户而不是 Web 应用程序帐户,这样就不需要登录特定的 gmail 帐户来从谷歌驱动器中检索文件。
如果有人能指出代码的哪一部分应该更改为使用服务帐户并仍然保留从谷歌驱动器检索文件的功能,那将非常有帮助。
<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cerebra", $con);
$sql="select name from details order by download desc limit 20";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$query=mysql_query($sql,$con);
$names=array();
$index=0;
while($row=mysql_fetch_array($query)){
$names[$index]=$row[0];
$index++;
}
foreach($names as $a)
echo $a;
?>
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var CLIENT_ID = '106997880011-je955vl71s3hbj7vh03oh1df1h5l9u9q.apps.googleusercontent.com';
var API_KEY = 'AIzaSyDYksh35q9jS1_YumnjWYR-717AoCKSXhM';
var SCOPES = 'https://www.googleapis.com/auth/drive';
function handleClientLoad() {
gapi.client.setApiKey(API_KEY);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
var options = {
client_id: CLIENT_ID,
scope: SCOPES,
immediate: true
};
gapi.auth.authorize(options, handleAuthResult);
}
function handleAuthResult(authResult) {
<!-- var authorizeButton = document.getElementById('authorize-button');
if (authResult && !authResult.error) {
<!--authorizeButton.style.visibility = 'hidden';
makeApiCall();
} else {
<!-- authorizeButton.style.visibility = '';
handleAuthClick;
}
}
function handleAuthClick(event) {
var options = {
client_id: CLIENT_ID,
scope: SCOPES,
immediate: false
};
gapi.auth.authorize(options, handleAuthResult);
return false;
}
function makeApiCall() {
gapi.client.load('drive', 'v2', makeRequest);
}
function makeRequest() {
var request = gapi.client.drive.files.list();
request.execute(function(resp) {
for (i=0; i<resp.items.length; i++) {
var titulo = resp.items[i].title;
var fechaUpd = resp.items[i].modifiedDate;
var userUpd = resp.items[i].lastModifyingUserName;
var userEmbed = resp.items[i].embedLink;
var userAltLink = resp.items[i].alternateLink;
var download = resp.items[i].webContentLink;
var hold="Download";
<!-- var fileInfo = document.createElement('li');
<!-- fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd +' url: ' + hold.link(download)));
<!-- document.getElementById('content').appendChild(fileInfo);
document.write(titulo + " ");
document.write(hold.link(download) + "<br>");
}
});
}
$(document).ready(function() {
<!-- $('#authorize-button').on('click',
handleAuthClick;
$.getScript('//apis.google.com/js/api.js', function() {
gapi.load('auth:client', handleClientLoad);
});
});
</script>
</head>
<body>
</body>
</html>