我正在使用 google-api-php-client 从客户端 0auth 流请求访问令牌,但它返回错误“无法从请求确定客户端 ID”。
我的代码如下。
我的 client_secret.json:
"web": {
"client_id":"123",
"project_id":"1234",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"123456789",
"redirect_uris":[
"http://localhost:7777/v1/oauth2callback"
],
"javascript_origins":[
"http://localhost:7777"
]
}
}
Run Code Online (Sandbox Code Playgroud)
我的身份验证 URL 创建:
$client->setAuthConfig(base_path() . '/client_secret.json');
$client->addScope(\Google_Service_Calendar::CALENDAR);
$client->setRedirectUri(URL::to('/') . '/v1/oauth2callback');
$client->setAccessType('offline');
$client->setPrompt('consent');
$client->setIncludeGrantedScopes(true);
$url = $client->createAuthUrl();
print filter_var($url, FILTER_SANITIZE_URL);
Run Code Online (Sandbox Code Playgroud)
然后我手动转到 URL:
身份验证流程似乎一切正常,并将我返回到我尝试解码令牌的网址:
$code = $request->code;
$client = new Google\Client();
$accessToken = $client->fetchAccessTokenWithAuthCode($code);
var_dump($accessToken);
exit;
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:
array(2) { ["error"]=> string(15) "invalid_request" ["error_description"]=> string(43) "无法根据请求确定客户端 ID。" }
google-api google-api-client google-oauth google-api-php-client
对工作在与谷歌注册,使用PHP库V2,OAuth 2.0用户.
经过很长一段时间花在它上面我最终配置,它工作一次很好,但之后它没有工作.点击登录链接后,用户需要对Google进行身份验证.工作正常,直到认证完成.
在使用GET变量回调时:
?code=something&authuser=0&prompt=consent&session_state=something
Run Code Online (Sandbox Code Playgroud)
它产生以下错误:
致命错误:未捕获的异常"Google_AuthException",消息"在C:\ xampp\htdocs\test\google-api-php-client\src\auth\Google_OAuth2.php中收到OAuth2访问令牌错误消息:'invalid_client'':115堆栈跟踪:#0 C:\ xampp\htdocs\test\google-api-php-client\src\Google_Client.php(127):Google_OAuth2-> authenticate(Array,4/xUGSPbXR0LNzW ...')#1C:\xampp\htdocs\test\google-api-php-client\index.php(40):Google_Client-> authenticate('4/xUGSPbXR0LNzW ...')#2 {main}抛出C:\ xampp\htdocs \第115行的test\google-api-php-client\src\auth\Google_OAuth2.php
使用以下代码(它将帮助您理解):
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_PlusService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
$plus = new Google_PlusService($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$me = $plus->people->get('me');
$url = filter_var($me['url'], FILTER_VALIDATE_URL);
$img = …Run Code Online (Sandbox Code Playgroud) require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
//First, build a Drive service object authorized with the service accounts
$auth = new Google_AssertionCredentials(
DRIVE_SERVICE_ACCOUNT_EMAIL,
array( DRIVE_SCOPE ),
file_get_contents( DRIVE_SERVICE_ACCOUNT_KEY )
);
$client = new Google_Client();
$client->setUseObjects( true );
$client->setAssertionCredentials( $auth );
$service = new Google_DriveService( $client );
//Then, insert the file
$file = new Google_DriveFile();
$file->setTitle( 'My document' );
$file->setMimeType( 'text/plain' );
$createdFile = $service->files->insert( $file, array(
'data' => 'Hello world!',
'mimeType' => 'text/plain',
));
print_r( $createdFile …Run Code Online (Sandbox Code Playgroud) google-api google-api-client google-drive-api google-api-php-client
我试图更新使用YouTube上的视频缩略图YouTube API,与 google-api-php-client库.我们正在上版1.0.3-beta.
我已经复制/粘贴了这个示例,并将我们client_id和client_secret我知道的值更改为了工作,因为我现在仍然可以使用这些值上传视频,我只是无法设置缩略图(我刚刚上传的视频使用了相同的凭据).
这是我得到的错误:
An client error occurred: Error calling PUT https://www.googleapis.com/upload/youtube/v3/thumbnails/set?videoId=wL3Rt0sokdI&uploadType=resumable&upload_id=AEnB2UqFI4w1qO1gjoFS-fOlc61OthOht0ngdFAcdG9Nnufds61IyMIUUxf1rAvmoDl77LeEQZb1-HMyqtc9Op3Jg4hRfcyuOQ: (403) Forbidden
Run Code Online (Sandbox Code Playgroud)
编辑 -更多信息:这是打印出整个异常时获得的一小部分信息:
{
["errors":protected]=> array(1)
{
[0]=> array(5) {
["domain"]=> string(17) "youtube.thumbnail"
["reason"]=> string(9) "forbidden"
["message"]=> string(9) "Forbidden"
["locationType"]=> string(6) "header"
["location"]=> string(13) "Authorization"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的脚本(它与示例代码相同,但我将它放在这里,以防上面的示例URL出现故障):
/**
* This sample uploads and sets a custom thumbnail for a video.
*
* 1. It uploads an image …Run Code Online (Sandbox Code Playgroud) 当使用Google Contacts API示例simple.php和GitHub版本1.0.4-beta中的Google API PHP客户端中描述时,我收到以下错误:
Fatal error: Call to undefined method Google_IO_Curl::authenticatedRequest()
Run Code Online (Sandbox Code Playgroud)
但是,当我改变这条线时......
$val = $client_svc_contacts->getIo()->authenticatedRequest($req);
Run Code Online (Sandbox Code Playgroud)
...至...
$val = $client_svc_contacts->getAuth()->authenticatedRequest($req);
Run Code Online (Sandbox Code Playgroud)
......然后它又开始工作了.
我使用版本1.0.4-beta未修改,除了在Client.php顶部添加以下行:
set_include_path(str_replace('/Google','',dirname(__FILE__)));
Run Code Online (Sandbox Code Playgroud)
我认识到simple.php是为0.6版编写的,而不是v1 +,但是与GitHub上的版本相比,这个例子是否过时?或者我的实施有问题吗?
我正在使用google api客户端获取博客文章列表(请参见以下代码)。
try {
// create service and get data
$blogger = new \Google_Service_Blogger($this->client);
// https://developers.google.com/blogger/docs/3.0/reference/posts/list
// GET https://www.googleapis.com/blogger/v3/blogs/blogId/posts
if ($this->cache->contains('posts')) {
$posts = $this->cache->fetch('posts');
}
else {
$posts = $blogger->posts->listPosts($this->bloggerId, $optParams);
$this->cache->save('posts', $posts, 600); // 600 = 10 mins
}
return $posts;
} catch (apiServiceException $e) {
// Error from the API.
//print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
} catch (Exception $e) {
//print 'There was a general error …Run Code Online (Sandbox Code Playgroud) 我之前从未使用过Goodle API,现在我尝试通过API访问Google日历.
我下载了google-api-php-client-master.zip,解压缩了.../src/Google目录并将其复制到我的网络服务器(由第三方托管,这意味着我无法安装任何东西).根据样本,我的代码需要从头开始
<?php
require_once "Google/Client.php";
require_once "Google/Service/Calendar.php";
....
Run Code Online (Sandbox Code Playgroud)
但是Client.php抛出一个错误:
致命错误:require_once():在第18行的/homepages/39/d396519017/htdocs/VC2/Google/Client.php中打开所需的''(include_path ='.:/ usr/lib/php5.4')失败
Client.php-Line 18就是这一行 require_once realpath(dirname(__FILE__) . '/../../autoload.php');
但是我无法在任何地方找到autoload.php.我错过了什么?
谢谢!
我正在尝试创建一个新的电子表格,据我了解,现在可以在API的v4中使用它,但是在PHP文档中没有看到任何有关如何创建工作表的详细信息。我有:
$client->setApplicationName("Test");
$client->setScopes(['https://www.googleapis.com/auth/sheets']);
$service = new Google_Service_Sheets($client);
$service->spreadsheets->create();
Run Code Online (Sandbox Code Playgroud)
但是它期望参数1是Google_Service_Sheets_Spreadsheet的实例,并且我不确定应该提供什么。
有没有办法以编程方式获取Google表单的可共享链接?我已经使用php api复制了一个Google表单文件,现在我想获取该表单的可共享链接。
google-api google-apps-script google-api-php-client google-forms
我正在使用Google的php api客户端。我正在浏览服务帐户快速入门指南。据我所知,我完美地遵循了这些步骤。我遇到以下错误:
{
"error": "invalid_grant",
"error_description": "Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values and use a clock with skew to account for clock differences between systems."
}
Run Code Online (Sandbox Code Playgroud)
据我了解,此错误最常见的问题是系统时间是否错误。我已经三重检查了我的时区,日期和时间是否与原子钟同步。我使用php set timezone函数来设置我的时区以匹配我的计算机,但是我继续遇到错误。我正在查看消息的另一部分,其中提到了iat和exp设置,但还没有到位。
有谁对我如何超越这个有任何想法?
google-api ×6
php ×5
google-oauth ×2
github ×1
google-forms ×1
oauth-2.0 ×1
symfony ×1
thumbnails ×1
youtube ×1
youtube-api ×1