如何使用服务帐户而不是谷歌驱动器api的Web应用程序

时间:2013-03-06 07:33:56

标签: javascript google-drive-api

以下javascript代码使用Web应用程序帐户访问驱动器API。我需要一些帮助来修改代码,以便它可以使用整个应用程序的服务帐户而不是Web应用程序帐户,这样就不需要登录到特定的Gmail帐户来从谷歌驱动器中检索文件。

如果有人可以指出应该更改代码的哪个部分以使用服务帐户并仍然保留从Google驱动器检索文件的功能,那将非常有用。

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

1 个答案:

答案 0 :(得分:0)

查看我的博客 - 我刚刚完成了一篇关于类似用例的文章 - 示例是针对Java服务器端逻辑的,但是对所有服务器端语言通用的特定步骤进行了详细的一般性解释:

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