1

以下 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 + "&nbsp;");
                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>
4

1 回答 1

0

看看我的博客——我刚刚完成了一篇关于类似用例的文章——示例适用于 Java 服务器端逻辑,但对所有服务器端语言通用的特定步骤有详细的一般说明:

http://devmint.blogspot.cz/2013/03/access-application-google-drive-account.html

于 2013-03-20T18:11:21.833 回答