我正在运行以下PHP代码,使用此处的客户端库:https://code.google.com/p/google-api-php-client/.我没有得到任何这个代码的错误,但是当我调用时getAccessToken(),它返回null.
我允许在个人日历上访问此服务帐户,并通过API控制台授予对项目的完全访问权限.
有任何想法吗?
require_once 'google-api-php-client/src/Google_Client.php';
const CLIENT_ID = 'blahblahblah';
const SERVICE_ACCOUNT_NAME = 'blahblahblah@developer.gserviceaccount.com';
const KEY_FILE = 'path/to/privatekey.p12';
$google_client = new Google_Client(); // created only to initialized static dependencies
$client = new Google_OAuth2(); // you really just need Google_OAuth2
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(
new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key
)
);
var_dump($client->getAccessToken());
Run Code Online (Sandbox Code Playgroud) 我正在使用Google API Client Library for PHP进行离线身份验证,但是当我使用此代码时:
$this->google->refreshToken($result["google_refresh_token"]));
Run Code Online (Sandbox Code Playgroud)
它返回NULL.
$this->google 指的是Google_Client类的一个实例.
任何人都知道这可能是什么?
如果我尝试更改给定的刷新令牌,则会返回Google异常错误,因此它应该是有效的令牌.
谢谢!
编辑:
'client_id' => 'myclientid',
'client_secret' => 'myclientsecret',
'redirect_uri' => 'mypage',
'developer_key' => 'mydeveloperkey',
// Other parameters.
'access_type' => 'offline',
'approval_prompt' => 'force',
Run Code Online (Sandbox Code Playgroud)
也许这有助于你
所以我在使用GAPI上传PHP时遇到了奇怪的问题.该文件实际上是在驱动器上创建的,但由于某种原因,数据不会传到谷歌,它只是创建一个0字节的文件.
这是我的代码:
function uploadFile($service, $title, $description, $parentId, $mimeType, $filepath) {
$mimeType = "image/png";
$title = "test.png";
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
// Set the parent folder.
if ($parentId != null) {
$parent = new Google_Service_Drive_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
try {
$data = file_get_contents();
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
//return $createdFile;
} catch (Exception $e) { …Run Code Online (Sandbox Code Playgroud) 我正在使用php watch命令:
$service = new Google_Service_Calendar($client);
$channel = new Google_Service_Calendar_Channel($client);
$channel->setId('20fdedbf0-a845-11e3-1515e2-0800200c9a6689111');
$channel->setType('web_hook');
$channel->setAddress('https://www.exampel.com/app/notification');
$watchEvent = $service->events->watch('primary', $channel);
Run Code Online (Sandbox Code Playgroud)
这个命令工作正常,我得到响应:
Google_Service_Calendar_Channel Object ( [address] => [expiration] => 1401960485000 [id] => 20fdedbf0-a845-11e3-1515e2-0800200c9a6689111 [kind] => api#channel [params] => [payload] => [resourceId] => HZjSdbhwcd5KMKEA3ATA31LoR-w [resourceUri] => https://www.googleapis.com/calendar/v3/calendars/primary/events?key=AIzaSyBl_Y7Y4eQDve-0DjwzBEP7_qOLo-67ouY&alt=json [token] => [type] => [modelData:protected] => Array ( ) [processed:protected] => Array ( ) )
Run Code Online (Sandbox Code Playgroud)
然而; 在我的设置网址中,当日历中发生某些更改时,我没有收到任何消息。我错过了什么吗?
我可以通过API Explorer成功向Youtube Analytics API发出请求.我的代码正在尝试使用Google PHP客户端库,特别是Google_Service_YouTubeAnalytics类.不幸的是,这个课程没有文档.
我在客户端上设置ID和断言凭据.我相信这是正常的,因为如果我将私钥更改为我知道不正确的内容,我会得到:
{"code":400,"error":"Error refreshing the OAuth2 token, message: '{\n \"error\" : \"invalid_grant\"\n}'"}
但是当我插入正确的私钥时,我得到以下响应:
{"code":400,"error":"Error calling GET https:\/\/www.googleapis.com\/youtube\/analytics\/v1\/reports?ids=channel%3D%3DCHANNEL_ID&start-date=2014-09-01&end-date=2014-09-05&metrics=views%2Cuniques: (400) Invalid query. Query did not conform to the expectations."}
它没有告诉我什么是关于查询的无效(这将是非常有用的),所以我不知道我可能做错了什么.任何帮助表示赞赏.
这是我发出请求的代码:
$client = new \Google_Client();
$client->setApplicationName(self::APP_NAME);
// set some stuff
$client->setClientId( self::CLIENT_ID );
$client->setClientSecret( self::CLIENT_SECRET );
$client->setAssertionCredentials(new \Google_Auth_AssertionCredentials(
self::CRED_ID,
[
"https://www.googleapis.com/auth/youtube.readonly",
'https://www.googleapis.com/auth/yt-analytics.readonly'
],
self::youtubeKey()
));
$youtubeService = new \Google_Service_YouTubeAnalytics($client);
$resp = $youtubeService->reports->query(
self::CHANNEL_ID,
'2014-09-01',
'2014-09-05',
'views,uniques'
);
Run Code Online (Sandbox Code Playgroud) 自2015年4月20日起,Google Documents API v3.0已弃用,并且在此日期之后将不再运行功能.因此,使用此API的任何人都必须切换为使用Google Drive API.
我已将Google Drive API集成到我的PHP应用程序中,但我无法找到如何为我创建或上传的文件获取EDIT网址.以前在上传文件后的Google Documents API中,响应会返回一个编辑网址,该编辑网址将是编辑该文件的直接网址.
我使用的是一个服务帐户,该帐户使用我的Google开发者帐户生成的密钥,网址为https://console.developers.google.com.这意味着我的应用程序代表开发人员帐户为我创建的服务帐户拨打电话.Drive UI服务帐户无法通过云端硬盘用户界面访问,因为作为用户,您无法像使用个人Google帐户一样登录该帐户.
我所做的是与我的个人帐户共享我上传或创建的文档,并且通话中的url google返回名为"alternateLink",格式如下:https: //docs.google.com/file/ d/0B0nkjw07ekP9LUpuZG4tOHcxdkE /编辑?USP = drivesdk
但是,当我登录到我共享上述文件的帐户时,它只会转到查看者,而不是"Google文档编辑器"
如何获取Google Drive API的文件编辑链接?
google-docs google-docs-api google-drive-api google-api-php-client
我正在使用服务帐户身份验证来使用Google表格API创建Google表格.我想创建一个电子表格,并以某种方式允许我的团队打开它.
$private_key = file_get_contents($rootDir . '/config/googleApiCredentials.p12');
$client_email = 'project-names@positive-water-134xxx.iam.gserviceaccount.com';
$scopes = implode(' ', ["https://www.googleapis.com/auth/drive"]);
$credentials = new \Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
// tried once with additional constructor params:
// $privateKeyPassword = 'notasecret',
// $assertionType = 'http://oauth.net/grant_type/jwt/1.0/bearer',
// $sub = 'myMail@gmail.com
$this->googleClient = new \Google_Client();
$this->googleClient->setAssertionCredentials($credentials);
if ($this->googleClient->getAuth()->isAccessTokenExpired()) {
$this->googleClient->getAuth()->refreshTokenWithAssertion();
}
$service = new \Google_Service_Sheets($this->googleClient);
$newSheet = new \Google_Service_Sheets_Spreadsheet();
$newSheet->setSpreadsheetId('34534534'); // <- hardcoded for test
$response = $service->spreadsheets->create($newSheet);
print_r($response);
Run Code Online (Sandbox Code Playgroud)
工作表正在创建,我收到回复
[spreadsheetId] => 34534534
Run Code Online (Sandbox Code Playgroud)
但是,即使我在Google的开发者控制台中添加了作为所有者,编辑,作者和读者的项目权限,我也无法通过浏览器使用我的谷歌帐户打开此文件.浏览器说我没有办法,我可以联系管理员获取权限
有什么建议?是否有人管理过如何创建文档并为任何"人类"提供访问权限?
php google-api google-sheets google-api-php-client service-accounts
我google-api-php-client用于获取和插入 Google 日历事件。现在我想将系统上的事件与 Google 日历上的事件同步。
我想知道,如果我在 Google 日历中添加事件,它是否可以自动添加到我的系统中(可能带有一些回调 url),所以我不需要使用 CRON 作业在某个时间间隔内重新检查 Google 日历中的事件?
谢谢!
我有一段时间试图使用 Google 日历 API 将一个非常简单的事件添加到日历中,如果有人能指出我的(可能很明显的)问题,我会很高兴的。我正在使用我在这里找到的代码。我已经把代码放在“google-api-php-client/examples.calendar”目录下,在那里可以找到一个简单的例子。
<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('worked.html'); //I made a file called "worked.html" in the same directory that just says "it worked!"
$client->setDeveloperKey('SecretLongDeveloperKey');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$authUrl = $client->createAuthUrl();
if (!$client->getAccessToken()){
$event = new Google_Event();
$event->setSummary('Halloween');
$event->setLocation('The …Run Code Online (Sandbox Code Playgroud) 我在多个页面上使用 Google API 时遇到问题。来自 Google 的示例工作正常,但所有代码都在一页上。
我有两页。第一页,用户点击登录按钮,第二页,我使用 google api 拉取用户信息。
第一页:
<?php
########## Google Settings.. Client ID, Client Secret from https://cloud.google.com/console #############
$google_client_id = 'myid';
$google_client_secret = 'mysecret';
$google_redirect_url = 'http://www.myWebsite.com/secondPage.php'; //path to your script
$google_developer_key = 'mydeveloperkey';
//include google api files
require_once '../includes/Google/autoload.php';
//start session
session_start();
$client = new Google_Client();
$client->setClientId($google_client_id);
$client->setClientSecret($google_client_secret);
$client->setRedirectUri($google_redirect_url);
$client->addScope("https://www.googleapis.com/auth/drive");
$client->addScope("https://www.googleapis.com/auth/calendar");
$client->addScope("https://www.googleapis.com/auth/gmail.compose");
$client->addScope("https://www.googleapis.com/auth/plus.me");
$drive_service = new Google_Service_Drive($client);
$calendar_service = new Google_Service_Calendar($client);
$gmail_service = new Google_Service_Gmail($client);
/************************************************
If we're logging out we just need …Run Code Online (Sandbox Code Playgroud)