当前位置: 首页 > 知识库问答 >
问题:

在php中使用curl的OAuth 2.0

汪同
2023-03-14

我需要为OAuth2.0获取access_token和refresh_token来访问Google API,下面的php脚本应该返回一个带有access_token和refresh_token的json,如下所示:

{
  "access_token" : "####",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "####"
}

但是,php脚本只返回这个错误消息:

{
"error" : "invalid_request",
"error_description" : "Client must specify either client_id or client_assertion, not both"
}
$client_id = '###.apps.googleusercontent.com';
$redirect_uri = 'http://localhost/phpConnectToDB/csv/refreshFusionTable.php';
$client_secret = '###';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");

$code = $_REQUEST['code'];

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $code,
'client_id' => $clientID,
'client_secret' => $clientSecret,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
));

$data = curl_exec($ch);

var_dump($data);
curl --data "code=###&client_id=###.apps.googleusercontent.com&client_secret=###&redirect_uri=http://localhost/phpConnectToDB/csv/refreshFusionTable.php&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token

共有1个答案

彭博厚
2023-03-14
    $client_id = '###.apps.googleusercontent.com';
    $client_secret = '###';
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'code' => $code,
    'client_id' => $clientID,
    'client_secret' => $clientSecret,
    'redirect_uri' => $redirect_uri,
    'grant_type' => 'authorization_code'
    ));
    $client_id = '###.apps.googleusercontent.com';
    $redirect_uri = 'http://localhost/phpConnectToDB/csv/refreshFusionTable.php';
    $client_secret = '###';

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");
    
    curl_setopt($ch, CURLOPT_POST, TRUE);
    
    $code = $_REQUEST['code'];

    // This option is set to TRUE so that the response
    // doesnot get printed and is stored directly in
    // the variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'code' => $code,
    'client_id' => $client_id,
    'client_secret' => $client_secret,
    'redirect_uri' => $redirect_uri,
    'grant_type' => 'authorization_code'
    ));

    $data = curl_exec($ch);
    
    var_dump($data);

但我不得不说,google在这里提供了一个误导性的错误信息,因为我没有定义client_id或client_secret,错误信息是:

    {
    "error" : "invalid_request",
    "error_description" : "Client must specify either client_id or client_assertion, not both"
    }
 类似资料:
  • 本文向大家介绍php中curl使用指南,包括了php中curl使用指南的使用技巧和注意事项,需要的朋友参考一下 许多同学在第一次使用curl的时候感觉一个头两个大(包括我在内),看着这一条条的curl_setopt函数完全摸不着头脑,不过在你花10分钟看了我的介绍后相信你以后也能轻松戏耍php的curl了 首先,请看一个curl代码(花10秒钟,略看一遍,然后跳到后文) WTF,这到底是在做什么?

  • 问题内容: 如何使用cURL在PHP中进行RAW POST? 未经处理的原始帖子没有任何编码,我的数据存储在字符串中。数据应采用以下格式: 一种选择是手动编写要发送的整个HTTP标头,但这似乎不太理想。 无论如何,我是否可以仅将选项传递给curl_setopt(),这些选项表示使用POST,使用文本/纯文本以及从中发送原始数据? 问题答案: 我刚刚找到了解决方案,可以回答我自己的问题,以防其他人偶

  • 我用扫描,这是结果: 看起来他们只支持ECDHE,从的输出来看,没有ECDHE密码!所以我的问题是如何使用PHP CURL客户端连接到远程服务器? 这是我的示例PHP代码,它返回false并返回消息: “列表中的未知密码:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128

  • 本文向大家介绍PHP curl使用实例,包括了PHP curl使用实例的使用技巧和注意事项,需要的朋友参考一下 概述 本博客的前面两篇文章:curl和libcurl简介以及PHP中使用curl对PHP中curl的使用做了简单介绍,但是PHP中curl的使用却并不简单,尤其是curl的各种配置项,本篇文章会讲解几个PHP的实例,以便大家更好的理解curl。 实例:抓取页面 使用curl抓取页面相对来

  • 本文向大家介绍PHP中使用curl入门教程,包括了PHP中使用curl入门教程的使用技巧和注意事项,需要的朋友参考一下 概述 在我的上一篇文章“curl和libcurl简介”中简单的给大家介绍了curl相关的知识。这篇文章向大家介绍一下PHP中的curl扩展。 尽管在上一篇文章中,对curl和libcurl做了区分,也解释了某些相关的概念。同时,也知道了PHP中的curl扩展其实是对libcurl

  • 问题内容: 我想拥有一个独立的PHP类,在这里我想拥有一个通过cURL调用API并获取响应的函数。有人可以帮我吗? 谢谢。 问题答案: 只需使用下面的代码即可获得来自宁静的Web服务URL的响应,而我使用的是社交提及URL。