【问题标题】:Refresh Token for Access Token Google API: R Code访问令牌 Google API 的刷新令牌:R 代码
【发布时间】:2015-11-28 07:56:59
【问题描述】:

我正在尝试使用我的刷新令牌、客户端 ID 和客户端密码来检索使用 R 代码的 youtube api 的访问令牌。

这是 google 如何发布请求的示例。

POST /o/oauth2/token HTTP/1.1 Host: accounts.google.com Content-Type: application/x-www-form-urlencoded client_id=21302922996.apps.googleusercontent.com&client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&refresh_token=1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ&grant_type=refresh_token

这是我的 r 代码: 库(httr)

url<- paste("https://accounts.google.com/o/oauth2/token?client_id=", client_id, "&client_secret=", client_secret, "&refresh_token=", refresh_token, "&grant_type=access_token", sep="")
POST(url)

我不断收到这样的回复:

回复 [https://accounts.google.com/o/oauth2/token?client_id=xxxxxxxxxx&client_secret=xxxxxxxx&refresh_token=xxxxxxxxxxxxxxxxxxxxxx&grant_type=refresh_token]

日期:2015-09-02 16:43

状态:400

内容类型:application/json

尺寸:102 B

{ “错误”:“无效请求”, "error_description" : "缺少必需参数:grant_type"

有没有更好的方法来做到这一点?也许使用 RCurl?如果是这样,请求的格式是什么?我将不胜感激!

【问题讨论】:

    标签: r oauth refresh token youtube-analytics-api


    【解决方案1】:

    RAdwords 包具有检索刷新令牌的功能。如果您不想添加整个包,只需将以下代码添加到脚本中即可。

    refreshToken = function(google_auth) {
      # This function refreshes the access token. 
      # The access token deprecates after one hour and has to updated 
      # with the refresh token.
      #
      # Args:
      #   access.token$refreh_token and credentials as input
      # Returns:
      #   New access.token with corresponding time stamp
    
        rt = rjson::fromJSON(RCurl::postForm('https://accounts.google.com/o/oauth2/token', 
                               refresh_token=google_auth$access$refresh_token, 
                               client_id=google_auth$credentials$c.id,
                               client_secret=google_auth$credentials$c.secret, 
                               grant_type="refresh_token", 
                               style="POST",
                               .opts = list(ssl.verifypeer = FALSE)))
        access <- rt
    
        access
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 1970-01-01
      • 2019-03-16
      • 2014-07-15
      • 2017-06-09
      • 2020-09-06
      • 1970-01-01
      • 2013-06-14
      • 2014-12-04
      相关资源
      最近更新 更多