【问题标题】:invalid api keys permission denied error in google api using php使用 php 的 google api 中的无效 api 密钥权限被拒绝错误
【发布时间】:2019-02-13 02:35:11
【问题描述】:

我第一次使用谷歌 api 我的代码执行完美,甚至使用人员服务 api 给我数据...但第二次它给我以下错误

类型:Google_Service_Exception

Message: { "error": { "code": 403, "message": "请求丢失 一个有效的 API 密钥。", "errors": [ { "message": "请求缺少一个 有效的 API 密钥。”、“域”:“全局”、“原因”:“禁止”}]、 “状态”:“PERMISSION_DENIED”} }

我正在使用 codeignitor,这是重定向的 uri 控制器功能...在此先感谢,抱歉英语不好..

$client = $this->google_client_setup();
$client->authenticate($this->input->get('code'));
$access_token = $client->getAccessToken();
$people_service = new Google_Service_PeopleService($client);


$profile = $people_service->people->get(
    'people/me', array('personFields' => 'names,emailAddresses'));
$access_token = $access_token['access_token'];
$email = $profile['emailAddresses'][0]['value'];
$name =  $profile['names'][0]['displayName'];
$user = [
    'email' => $email,
    'name' => $name,
    'is_admin' => 0
];

【问题讨论】:

  • 如果没有看到控制器的其余部分并知道它是如何被调用的,很难判断,但很可能在第二次运行时您没有通过GET 获得code。这将导致 $client 无法正确设置,然后使用它的所有东西都会开始表现异常
  • 但我的代码第一次完美执行..刷新页面时出现此错误
  • 这正是我的意思...看来在第一次运行时您通过GET 正确传递了变量,但刷新页面时却没有

标签: php codeigniter google-api


【解决方案1】:

很难说,但我认为如果它说它缺少有效的 api 密钥,你应该把它当作答案;)

第一次运行后你是否被重定向到某个地方?这可以解释哈维尔评论的问题。

我建议转储两件事来调试代码:

$this->input->get('code')

如果没问题,那么

$access_token

这应该让你知道你的问题是什么:)

祝你好运!

【讨论】:

  • 两次都设置正确?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
相关资源
最近更新 更多