【问题标题】:How to get a Google API token获取 Google API 令牌
【发布时间】:2011-08-06 21:50:04
【问题描述】:

我需要获取 google 有效令牌才能使用 Google API,但是 我的代码不起作用。你能给我建议吗?

$client_id = '495225261106.apps.googleusercontent.com';
$client_secret = urlencode('MY_SECRET_CDE');
$redirect_uri = urlencode('http://MYPAGE.net/test.php');
//$grant_type = urlencode('authorization_code'); //it does not work either.
$grant_type = 'authorization_code';

$post_string = "code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&client_id={$client_id}&client_secret={$client_secret}&redirect_uri={$redirect_uri}&grant_type={$grant_type}";

//echo_key_value('post_string',$post_string);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);   // Execute the HTTP command
$errmsg = curl_error($ch); 

if($errmsg) echo $errmsg;

// 输出: {"错误":"invalid_grant"} //

谢谢!

【问题讨论】:

  • 我正在尝试在 javascript 中实现这一点,但遇到了同样的错误。我试过从命令行使用 curl ,但仍然没有运气。我已按照this page 上的说明进行操作。

标签: token oauth-2.0


【解决方案1】:

您可能会发现通过官方客户端库之一使用 Google API,尤其是 OAuth 东西更容易。

这是 PHP 的链接:http://code.google.com/p/google-api-php-client/

还有一个指向 OAuth 2.0 文档的链接(带有一些很棒的示例代码):http://code.google.com/p/google-api-php-client/wiki/OAuth2

【讨论】:

  • 我有同样的问题,无效授权,我正在使用浏览器发布数据以进行调试。使用他们的库并不能解释为什么会出现错误
【解决方案2】:

在使用 postfields 之前,您不必输入“ curl_setopt($ch, CURLOPT_POST, true); ”吗?我的工作正常,除了我没有在我的秘密上使用 urlencode,它是一样的

【讨论】:

    【解决方案3】:

    设置说明

    • 转到 Google Developers Console
      https://console.developers.google.com/project 选择您的项目或 创建一个新的(然后选择它)
    • 为您的 API 启用 项目在左侧边栏中,展开 APIs & auth > APIs Search 对于“驱动器”,单击“驱动器 API”,单击蓝色的“启用 API”按钮
    • 为您的项目创建服务帐户在左侧边栏中, 展开 APIs & auth > Credentials 点击蓝色的“Add credentials”按钮
    • 选择“服务帐户”选项
    • 选择“提供一个新的私人 键”复选框 选择“JSON”键类型选项
    • 点击蓝色的“创建” 按钮您的 JSON 密钥文件已生成并下载到您的机器 (这是唯一的副本!)
    • 打开 json 文件并将您的私钥保存到名为 rsa 的文件中

    记下您的服务帐户的电子邮件地址 (也可在 JSON 密钥文件中找到)与 使用上述电子邮件的服务帐户

    基于来自(一个很棒的文档)的信息 https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

    查看可能的 API 范围集列表 https://developers.google.com/identity/protocols/googlescopes#sheetsv4

    纯粹基于 bash 的解决方案

    #!/bin/bash
    
    client_email='your client email'
    scope='https://www.googleapis.com/auth/spreadsheets.readonly'
    
    jwt1=`echo -n '{"alg":"RS256","typ":"JWT"}' | openssl base64 -e`
    
    exp=$(($(date +%s)+3600))
    iat=$(date +%s)
    
    jwt2=`echo -n '{\
    "iss":"'"$client_email"'",\
    "scope":"'"$scope"'",\
    "aud":"https://accounts.google.com/o/oauth2/token",\
    "exp":'$exp',\
    "iat":'$iat'}' | openssl base64 -e`
    
    jwt3=`echo -n "$jwt1.$jwt2" | tr -d '\n' | tr -d '=' | tr '/+' '_-'`
    
    jwt4=`echo -n "$jwt3" | openssl sha -sha256 -sign rsa | openssl base64 -e`
    
    jwt5=`echo -n "$jwt4" | tr -d '\n' | tr -d '=' | tr '/+' '_-'`
    
    echo $jwt3
    echo $jwt5
    
    curl -H -vvv "Content-type: application/x-www-form-urlencoded" -X POST "https://accounts.google.com/o/oauth2/token" -d \
    "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=$jwt3.$jwt5"
    

    有关基于 javascript nodejs 的解决方案,请参阅

    https://gist.github.com/cloverbox/5ce51a1d8889da9045c5b128a3a2502f

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2013-03-05
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 2013-06-14
      • 2014-03-08
      • 2012-08-05
      • 2019-05-05
      相关资源
      最近更新 更多