首页 > 解决方案 > 保护公共 WP 插件中使用的 Google API 密钥

问题描述

今天是个好日子,

我计划构建一个需要 Google Search Console API 的 WP 插件。目标是不要将我的 API 密钥和客户端 ID 与插件一起发送。

  1. WP 插件 -> 从 domain.com 请求数据
  2. domain.com 从 Google Search Console API 请求数据
  3. domain.com 将数据发送到 WP 插件而不保存它
  4. WP Plugin 将数据保存在安装位置

我可以知道这是否可能以及我需要做什么?

谢谢,

编辑:2 月 16 日

我发现这可以通过从客户端验证用户来完成(wp插件)

require_once dirname(__FILE__) . '/vendor/autoload.php';

$client = new Google_Client();

$client->setScopes( array( 'https://www.googleapis.com/auth/webmasters', 'https://www.googleapis.com/auth/webmasters.readonly' ) );
$client->setAccessType( 'offline' );
$client->setRedirectUri( 'urn:ietf:wg:oauth:2.0:oob' );
$client->setClientId( '302343592020-r7ouc9s9jaukc54j48s3k9fg26eeiaqr.apps.googleusercontent.com' );
$client->setIncludeGrantedScopes(true);

//Generate access code from the link below
//echo '<a href="'.$client->createAuthUrl().'" target="_blank">Access Code</a>';

$code = '###############';

$client->authenticate($code); $access_token = $client->getAccessToken();

但是,访问令牌为空

标签: wordpressgoogle-search-console

解决方案


我会将这个问题标记为已解决。

我开始意识到 WP Plugin 可以通过我的域进行身份验证以获取访问令牌。然后使用访问令牌直接从 wp_plugin 调用 google api。

这样我就不会连同插件源代码一起发送我的客户端密码。


推荐阅读