duanbo6482 2014-07-25 23:01
浏览 27
已采纳

从php访问电子表格信息

I am trying to get a list of users from Google Spreadsheet using PHP running on Google appengine.

The strangest part is that I am using Google account login and restricted to my domain as explained in the link here.

https://developers.google.com/appengine/articles/auth

This is really awesome cause I don't have to configure anything to handle for people who do not have @mydomain.com and makes URL secure so that others cant access it.

No starts the problem. I am trying to access spreadsheet so I can get a list of users but it is not as easy as it seems. There topics about accesstoken and Oauth2.0 etc but I am not using any of those and there is no need to if you this option for user authentication.

How do I read info in a spreadsheet from this PHP file?

My app.yaml file for this file as below.

- url: /attendance/form.php
  script: attendance/Form/form.php
  login: required
  auth_fail_action: redirect

There must be a simple way to read info from a google spreadsheet and since its is PHP I can put my username and password into, Don't see a problem with it.

I also checked out https://developers.google.com/google-apps/documents-list but it does not have code for PHP and I don't know JAVA. Any one know what to do in this situation ?

  • 写回答

2条回答 默认 最新

  • duancan1950 2014-08-07 00:51
    关注

    After a lot of head banging and guess work I figured it out.

    Followed the instructions in here

    https://github.com/google/google-api-php-client

    • you don't need JSON for GAE version since it has PHP 5.4

    As for install library you have to download and put "Google" folder in your code folder, so that it uploads to GAE along with your app and then include the library with the following

    set_include_path(get_include_path() . PATH_SEPARATOR . 'Google/src');
    

    Then the doc says that you need to add APP Key

    $client->setDeveloperKey("YOUR_APP_KEY");
    

    I had no idea what the APP KEY is. I keep digging into the doc and keep saying to go to the developer console. I still don't know what is an APP KEY. Now trying to wing it from the error from the browser that said invalid key setup this

    enter image description here

    That API key is what the "YOUR_APP_KEY". I really wish someone explained that to me I spent days confused and lost. It was more confusing when it said server or browser. I still don't know if I should use browser or server. It is working now so I wont trouble with it.

    The getting started steps also fails to say that you need to do a require_once . I am newbie and docs need to explain step by step. I do not know what others may need but add whatever Service you need eg

    require_once 'Google/Client.php';
    require_once 'Google/Service/Drive.php';
    

    The link above does not even say the different services found in it. I have to look up the file name and then open it to find out the service name. Although it looks simple after doing all this it would be nice if they were listed on the documentation link it self. I still don't know what commands I need to pull out other stuff.

    Then after some mindless clicking around I found that we can find or know about the api functions by actually clicking on the in the console.

    https://console.developers.google.com/project/apps~<YOURAPPID>/apiui/api/drive -- cant format it sorry

    Then I cobbled up some code and ended up with help of others in stack

    try{
    $file = $service->files->get(FILEID);
    print "Title: " . $file->getTitle();
    print "Description: " . $file->getDescription();
    print "MIME type: " . $file->getMimeType();
    }
    
    catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
    

    FILEID is the long has value Google gives to each file. I figured I can test it using the online DriveAPI that allows to run querys and see what I get. I initially got error that the file was not found. It seems that I have to enable Oauth 2.0 for it to work.

    As of now I got into the system and know what type of file I can see. I hope this helps any one who is trying it out for the first time. I will try to update once I can read info out of the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题