douzhi7451 2018-08-16 15:38
浏览 64

使用PHP将数据添加到Google电子表格

I'm trying to create an automation script with php which adds a new row of information to a google sheet that I've shared. I've been scouring tutorials all morning trying to figure this out but each method is different and doesn't seem to be exactly what I need to do. Essentially this script will be triggers when a form is submitted on my site. Its going to take the form data and drop is into my google sheet.

I've created a service account under my project, which is stored inside the json file that was downloaded. Here's the script I have now. It's giving me the following error

{ "error": { 
  "code": 403, 
  "message": "The caller does not have permission", 
  "errors": [ { 
    "message": "The caller does not have permission", 
    "domain": "global", 
    "reason": "forbidden" 
  } ], 
  "status": "PERMISSION_DENIED"
}

and here's the code I'm running. (yes its incomplete because I can't get past the auth)

public function index()
{
    $this->load->helper("url");
    require APPPATH . 'third_party/GoogleAPI/vendor/autoload.php';


    putenv('GOOGLE_APPLICATION_CREDENTIALS=' . APPPATH . 'third_party/GoogleAPI/bdgsheets.json');
    $client = new Google_Client;
    $client->useApplicationDefaultCredentials();

    $client->setApplicationName("Is this working");
    $client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);
    $client->setAccessType('offline');

    if ($client->isAccessTokenExpired()) {
        $client->refreshTokenWithAssertion();
    }

    $accessToken = $client->fetchAccessTokenWithAssertion()["access_token"];

    $sheets = new Google_Service_Sheets($client);

    $data = [];
    $current_row = 2;
    $spreadsheet_id = "{my spreadsheet id here}";
    $range = 'A1:H';
    $rows = $sheets->spreadsheets_values->get($spreadsheet_id, $range, ['majorDimension' => 'ROWS']);
}

Thanks in advance for the help

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度