我正在使用google-api-php-client 0.6.1而且我想知道有没有办法冒充具体用户的服务帐号?我的应用程序需要在其谷歌硬盘中存储一些文件.所以,我决定使用服务帐户和.p12密钥 - 验证.它运行良好,但所有文件都存储在服务帐户中,因此我无法管理它们.我希望将文档存储在特定帐户(用于创建api项目和服务帐户本身).我试图使用这段代码:
$KEY_FILE = <p12 key file path>;
$key = file_get_contents($KEY_FILE);
$auth = new Google_AssertionCredentials(
$SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'),
$key);
$auth->prn = '<certainuser@gmail.com>';
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
return new Google_DriveService($client);
Run Code Online (Sandbox Code Playgroud)
但是我收到了"刷新OAuth2令牌时出错,消息:'{"error":"access_denied"}'"
我正在运行以下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) 我可以使用下面的代码创建子文件夹,但我只是无法返回id.
我想获取文件夹的id和/或链接,以便我可以将它们添加到我的数据库中.
目前,下面创建了文件夹,但只返回"//"作为id!
谢谢你的帮助
<?php
if ($client->getAccessToken()) {
$file = new Google_DriveFile();
//Setup the Folder to Create
$file->setTitle('Project Folder');
$file->setDescription('A Project Folder');
$file->setMimeType('application/vnd.google-apps.folder');
//Set the ProjectsFolder Parent
$parent = new Google_ParentReference();
$parent->setId('0B9mYBlahBlahBlah');
$file->setParents(array($parent));
//create the ProjectFolder in the Parent
$createdFile = $service->files->insert($file, array(
'mimeType' => 'application/vnd.google-apps.folder',
));
// print_r($createdFile);
print "folder created sucessfully";
echo "folder id is" . $createdFile->id;
}
?>
Run Code Online (Sandbox Code Playgroud) 我在google-api-client-php库中使用了user-example.php,它正在抛出错误
Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Could not json decode the token' in /Applications/XAMPP/xamppfiles/htdocs/Calendar/google-api-php-client/src/Google/Auth/OAuth2.php:174 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/Calendar/google-api-php-client/src/Google/Client.php(196): Google_Auth_OAuth2->setAccessToken('[]') #1 /Applications/XAMPP/xamppfiles/htdocs/calendar/google-api-php-client/examples/user-example.php(80): Google_Client->setAccessToken('[]') #2 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/Calendar/google-api-php-client/src/Google/Auth/OAuth2.php on line 174
Run Code Online (Sandbox Code Playgroud)
示例的代码是
<?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 …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)
然而; 在我的设置网址中,当日历中发生某些更改时,我没有收到任何消息。我错过了什么吗?
我想将我的令牌字符串存储在mysql数据库中.我不确定google-api-php-client库中的Client :: getAccessToken函数返回的令牌的最大大小https://github.com/google/google-api-php-client
现在我对值执行了print_r并注意到长度为168.这使我将令牌存储为类型varchar和长度200.
关于google-api-php-client库中getAccessToken函数返回的访问令牌字符串的最大长度,我将非常感谢.我希望将这些信息存储在mysql数据库中.
我收到以下错误:
OAuth 2.0访问令牌已过期,并且刷新令牌不可用.对于自动批准的响应,不会返回刷新令牌
我有一个只有我的服务器才能访问的Web应用程序,初始身份验证工作正常,但一小时后,弹出上述错误消息.我的脚本看起来像这样:
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("{MY_API_KEY}");
$client->setClientId('{MY_CLIENT_ID}.apps.googleusercontent.com');
$client->setClientSecret('{MY_KEY}');
$client->setRedirectUri('{MY_REDIRECT_URI}');
$client->setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
Run Code Online (Sandbox Code Playgroud)
如何为此设置刷新令牌?
编辑1:我也尝试过使用服务帐户.我按照GitHub上提供的文档:
https://github.com/google/google-api-php-client/blob/master/examples/service-account.php
我的脚本看起来像这样:
session_start();
include_once "templates/base.php";
require_once 'Google/Client.php';
require_once 'Google/Service/Gmail.php';
$client_id = '{MY_CLIENT_ID}.apps.googleusercontent.com';
$service_account_name = '{MY_EMAIL_ADDRESS}@developer.gserviceaccount.com ';
$key_file_location = 'Google/{MY_KEY_FILE}.p12';
echo pageHeader("Service Account Access");
if …Run Code Online (Sandbox Code Playgroud) 我试图在php中发布google plus时刻的帖子,我使用下面的代码,但是当试图发布我收到授权错误时,我正在使用codeignitter
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
include_once 'base_controller.php';
class Gp_auth extends Base_controller {
private $_gp_client;
public function __construct() {
parent::__construct();
$this->load->library('googleplus');
$this->_gp_client = $this->googleplus->client;
}
public function index() {
if ($this->input->get_post('code')) {
try {
$this->_gp_client->authenticate($this->input->get_post('code'));
$access_token = $this->_gp_client->getAccessToken();
$this->session->set_userdata('access_token', $access_token);
redirect('/gp_auth/me');
} catch (Google_Auth_Exception $e) {
_print($e);
}
} else {
$this->_gp_client->addScope(array(
'https://www.googleapis.com/auth/plus.login',
'https://www.googleapis.com/auth/plus.moments.write'
));
$this->_gp_client->setRequestVisibleActions('http://schemas.google.com/AddActivity');
try {
echo anchor($this->_gp_client->createAuthUrl(), 'Conect Me');
} catch (Google_Auth_Exception $e) {
_print($e);
}
}
}
public function me() {
try …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过此处的说明实现Google的登录API ,但出于某种原因,当我尝试运行它时,我总是得到:
致命错误:第76行/home/.../Google/Client.php中未找到"Google_Config"类
我很确定我正确链接到Client.php - 我不认为这是我自己的代码中的问题.有谁知道这里会发生什么?谢谢!
我们将YouTube Live Streaming API与Google API PHP客户端结合使用,我无法弄清楚如何使用基本(预设)提取而不是自定义提取.
自定义的是正常的,但由于某些原因,即使您将它们称为相同的名称,它也会为您创建的每个流不断创建重复项.
所以我的问题是,我们如何让它使用基本的摄取或者能够选择一个自定义的,而不是每次都创建一个新的?
例如,您可以在YouTube帐户中手动设置流时选择基本提取:
相关的PHP代码:
// Create an object for the liveBroadcast resource's snippet. Specify values
// for the snippet's title, scheduled start time, and scheduled end time.
$broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
$broadcastSnippet->setTitle($this->title);
$broadcastSnippet->setDescription($this->desc);
$broadcastSnippet->setScheduledStartTime($this->start_time);
// Create an object for the liveBroadcast resource's status, and set the
// broadcast's status.
$status = new Google_Service_YouTube_LiveBroadcastStatus();
$status->setPrivacyStatus($this->privacy_status);
// Create the API request that inserts the liveBroadcast resource.
$broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
$broadcastInsert->setSnippet($broadcastSnippet);
$broadcastInsert->setStatus($status); …Run Code Online (Sandbox Code Playgroud) youtube youtube-api google-api-php-client youtube-data-api youtube-livestreaming-api
php ×7
google-api ×3
google-oauth ×3
codeigniter ×1
gmail ×1
google-plus ×1
json ×1
oauth-2.0 ×1
youtube ×1
youtube-api ×1