如何以编程方式获取访问令牌以使用 PHP 向 Google Merchant Center(Google 购物)发送 API 调用?
How to programmatically get the access token to send API call to Google Merchant Center (Google Shopping) with PHP?
我最近与 Google 商家产品取得联系,将我所有的网站产品同步到 Google 商家。当我遵循 API 文档 https://developers.google.com/shopping-content/v2/quickstart 的结构时
到授权部分,我复制了他们的库并复制了要使用的示例代码。它确实有效!但是,当我进行测试以加载该身份验证页面时,它要求我登录开发人员帐户以获取访问令牌并将其保存到会话中。
有没有可能我可以跳过登录部分自动登录然后我可以让玉米系统运行每小时同步一次(更新产品的详细信息)?
我尝试将我的帐户登录 API 键入我的代码,如下所示:
$client = new Google_Client();
$client->setApplicationName('Sample Content API application');
//add my api key here
$client->setDeveloperKey(MY_API_KEY);
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setScopes('https://www.googleapis.com/auth/content');
但是还是不行,还是需要登录。
你想为此使用 service account。
The Google OAuth 2.0 system supports server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.)
下面是通过 服务帐户 和 PHP.
进行身份验证的工作示例
先决条件
此时您将获得身份验证所需的JSON文件。
根据Google的建议:
Important: Protect the *.json key file that allows a service account
to access the Google services for which it has been authorized. It is
good practice to allow service accounts to only access one Google API
each. This is a preventative measure to mitigate the amount of data an
attacker can access in the situation that the service account’s *.json
key file is compromised.
您现在可以使用以下函数获取 API 调用的访问令牌:
// gets access token for API call to Google Merchant Center
function gets_access_token_for_API_call() {
// load the "Google APIs Client Library for PHP"
require_once ( '/path/to/google-api-php-client/vendor/autoload.php' );
// gets the JSON key of the service account
$credentialsFilePath = '/path/to/merchant-center-123456789-987654321.json';
$client = new Google_Client();
$client->setAuthConfig($credentialsFilePath);
$client->addScope( 'https://www.googleapis.com/auth/content' );
// fetches a fresh access token with a given assertion token.
$client->fetchAccessTokenWithAssertion(); // the deprecated alias: "refreshTokenWithAssertion()"
$token = $client->getAccessToken();
return $token;
}
返回的结果将是一个数组,类似于:
{
"access_token": "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M",
"scope": "https://www.googleapis.com/auth/content"
"token_type": "Bearer",
"expires_in": 3600
}
代码已经过测试并且有效。
我最近与 Google 商家产品取得联系,将我所有的网站产品同步到 Google 商家。当我遵循 API 文档 https://developers.google.com/shopping-content/v2/quickstart 的结构时 到授权部分,我复制了他们的库并复制了要使用的示例代码。它确实有效!但是,当我进行测试以加载该身份验证页面时,它要求我登录开发人员帐户以获取访问令牌并将其保存到会话中。
有没有可能我可以跳过登录部分自动登录然后我可以让玉米系统运行每小时同步一次(更新产品的详细信息)?
我尝试将我的帐户登录 API 键入我的代码,如下所示:
$client = new Google_Client();
$client->setApplicationName('Sample Content API application');
//add my api key here
$client->setDeveloperKey(MY_API_KEY);
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setScopes('https://www.googleapis.com/auth/content');
但是还是不行,还是需要登录。
你想为此使用 service account。
The Google OAuth 2.0 system supports server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.)
下面是通过 服务帐户 和 PHP.
进行身份验证的工作示例先决条件
此时您将获得身份验证所需的JSON文件。
根据Google的建议:
Important: Protect the *.json key file that allows a service account to access the Google services for which it has been authorized. It is good practice to allow service accounts to only access one Google API each. This is a preventative measure to mitigate the amount of data an attacker can access in the situation that the service account’s *.json key file is compromised.
您现在可以使用以下函数获取 API 调用的访问令牌:
// gets access token for API call to Google Merchant Center
function gets_access_token_for_API_call() {
// load the "Google APIs Client Library for PHP"
require_once ( '/path/to/google-api-php-client/vendor/autoload.php' );
// gets the JSON key of the service account
$credentialsFilePath = '/path/to/merchant-center-123456789-987654321.json';
$client = new Google_Client();
$client->setAuthConfig($credentialsFilePath);
$client->addScope( 'https://www.googleapis.com/auth/content' );
// fetches a fresh access token with a given assertion token.
$client->fetchAccessTokenWithAssertion(); // the deprecated alias: "refreshTokenWithAssertion()"
$token = $client->getAccessToken();
return $token;
}
返回的结果将是一个数组,类似于:
{
"access_token": "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M",
"scope": "https://www.googleapis.com/auth/content"
"token_type": "Bearer",
"expires_in": 3600
}
代码已经过测试并且有效。