【问题标题】:Google API:Looking for the code of php classes needed for spredsheet API google installation or to work (cli not allowed)Google API:寻找spredsheet API google安装或工作所需的php类代码(不允许cli)
【发布时间】:2017-03-23 04:52:46
【问题描述】:

我查看了 Github 下载的 "happy shit" (:p) 或 API 表的 php 代码, 但代码示例似乎很奇怪。 我已经习惯了这样的 curl 类和 JSON 开放,在 Facebook 和 Twitter 上,但我真的不明白这里

我在服务器上没有 CLI 访问权限,因此无法通过这种方式安装。并且下载的代码不起作用/不能工作。

所以我查看了文档和 github#installation 上给出的示例代码:

require_once '/path/to/your-project/vendor/autoload.php'; =>好的,但是代码没有声明任何类。

#

代码 php

/**
 * THIS FILE IS FOR BACKWARDS COMPATIBILITY ONLY
 *
 * If you were not already including this file in your project, please ignore it
 */

$file = __DIR__ . '/../../vendor/autoload.php';

if (!file_exists($file)) {
  $exception = 'This library must be installed via composer or by downloading the full package.';
  $exception .= ' See the instructions at https://github.com/google/google-api-php-client#installation.';
  throw new Exception($exception);
}

$error = 'google-api-php-client\'s autoloader was moved to vendor/autoload.php in 2.0.0. This ';
$error .= 'redirect will be removed in 2.1. Please adjust your code to use the new location.';
trigger_error($error, E_USER_DEPRECATED);

require_once $file;

那么,请问基本示例如何工作? -https://github.com/google/google-api-php-client#installation

`// include your composer dependencies
require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("YOUR_APP_KEY");

$service = new Google_Service_Books($client);
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);

foreach ($results as $item) {
  echo $item['volumeInfo']['title'], "<br /> \n";
}
#

我查看了 client.php 文件,但它调用了未声明的类而不需要任何要求...

所以我的问题是如何从 php 开始? (我了解所需的凭据和 ID,获取 Access_token,...但是如何要求/声明这些类?)

我下载的src副本:https://drive.google.com/file/d/0B_hTHU7PDrJpX0ttWjd3RmtLZkU/view?usp=sharing

谢谢(这是为了工作......)

###############

Dalm 对 cme​​ts 的回答:

developers.../sheets/api/quickstart/php 这是我来自的页面。 并且代码来自给出的github链接

第 1 步:开启 Google Sheets API=OK 第 2 步:安装 Google 客户端库=如果没有 cli,如何在 cli 中运行 php? 第3步:当然给:

quicstart.php:

致命错误:未捕获的异常 'Exception' 并带有消息 '此库必须通过 composer 或下载完整包安装。请参阅 github.com/google/google-api-php-client#installation 上的说明。在 C:\Program Files\EasyPHP-12.1\www\leviet\pourFred\Google\autoload.php:14 堆栈跟踪:#0 C:\Program Files\EasyPHP-12.1\www\leviet\quickstart2.php(2): require_once() #1 {main} 在第 14 行的 C:\Program Files\EasyPHP-12.1\www\leviet\pourFred\Google\autoload.php 中抛出


所以我尝试了 exec 命令:
$output2 = shell_exec('php composer.phar require google/apiclient:^2.0');
$o= exec('php composer.phar require google/apiclient:^2.0',$output,$return);

print_r('output2:'.$output2);
print_r('o:'.$o);
passthru($o);
print_r('output:'.$output);
print_r('return:'.$return);

结果是:

output0:
o:
output    
return:1


我还尝试了 github 中的示例: github.com/google/google-api-php-client/blob/master/examples/simple-file-upload.php
<?php
/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("File Upload - Uploading a simple file");
/*************************************************
 * Ensure you've downloaded your oauth credentials
 ************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile()) {
  echo missingOAuth2CredentialsWarning();
  return;
}
/************************************************
 * The redirect URI is to the current page, e.g:
 * http://localhost:8080/simple-file-upload.php
 ************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client = new Google_Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
// add "?logout" to the URL to remove a token from the session
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['upload_token']);
}
/************************************************
 * If we have a code back from the OAuth 2.0 flow,
 * we need to exchange that with the
 * Google_Client::fetchAccessTokenWithAuthCode()
 * function. We store the resultant access token
 * bundle in the session, and redirect to ourself.
 ************************************************/
if (isset($_GET['code'])) {
  $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
  $client->setAccessToken($token);
  // store in the session also
  $_SESSION['upload_token'] = $token;
  // redirect back to the example
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// set the access token as part of the client
if (!empty($_SESSION['upload_token'])) {
  $client->setAccessToken($_SESSION['upload_token']);
  if ($client->isAccessTokenExpired()) {
    unset($_SESSION['upload_token']);
  }
} else {
  $authUrl = $client->createAuthUrl();
}
/************************************************
 * If we're signed in then lets try to upload our
 * file. For larger files, see fileupload.php.
 ************************************************/
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) {
  // We'll setup an empty 1MB file to upload.
  DEFINE("TESTFILE", 'testfile-small.txt');
  if (!file_exists(TESTFILE)) {
    $fh = fopen(TESTFILE, 'w');
    fseek($fh, 1024 * 1024);
    fwrite($fh, "!", 1);
    fclose($fh);
  }
  // This is uploading a file directly, with no metadata associated.
  $file = new Google_Service_Drive_DriveFile();
  $result = $service->files->create(
      $file,
      array(
        'data' => file_get_contents(TESTFILE),
        'mimeType' => 'application/octet-stream',
        'uploadType' => 'media'
      )
  );
  // Now lets try and send the metadata as well using multipart!
  $file = new Google_Service_Drive_DriveFile();
  $file->setName("Hello World!");
  $result2 = $service->files->create(
      $file,
      array(
        'data' => file_get_contents(TESTFILE),
        'mimeType' => 'application/octet-stream',
        'uploadType' => 'multipart'
      )
  );
}
?>

<div class="box">
<?php if (isset($authUrl)): ?>
  <div class="request">
    <a class='login' href='<?= $authUrl ?>'>Connect Me!</a>
  </div>
<?php elseif($_SERVER['REQUEST_METHOD'] == 'POST'): ?>
  <div class="shortened">
    <p>Your call was successful! Check your drive for the following files:</p>
    <ul>
      <li><a href="https://drive.google.com/open?id=<?= $result->id ?>" target="_blank"><?= $result->name ?></a></li>
      <li><a href="https://drive.google.com/open?id=<?= $result2->id ?>" target="_blank"><?= $result2->name ?></a></li>
    </ul>
  </div>
<?php else: ?>
  <form method="POST">
    <input type="submit" value="Click here to upload two small (1MB) test files" />
  </form>
<?php endif ?>
</div>

<?= pageFooter(__FILE__) ?>

但是'template/base.php'在下载的代码中的任何地方都不存在...

我发现最后一个问题: 在“master”下载中,只有“src”目录存在。 并且模板目录在示例库中是正常的(仅在版本中可用:“fixes-1122”和“v1-master”

【问题讨论】:

标签: php oauth oauth-2.0 google-oauth google-sheets-api


【解决方案1】:

所以,我回答了我的问题:

1.下载完整的 github(带有示例和 src 目录) https://github.com/google/google-api-php-client#installation 但不是 google-api-php-client-master 客户端

2.创建一个api密钥

“享受”

Ap(i)s 的王牌:起源!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-20
    • 2014-04-21
    • 1970-01-01
    • 2017-11-01
    • 2017-04-21
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多