用于检索令牌的 Youtube oAuth 2.0 API 永久代码 [英] Youtube oAuth 2.0 API permanent code to retrieve tokens

查看:33
本文介绍了用于检索令牌的 Youtube oAuth 2.0 API 永久代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的目标是制作一个表单,允许我网站的每个访问者将评论部分中的视频上传到我的 OWN 频道.目前我正在使用 YouTube OAuth 版本 3 API.问题是每 3600 秒,来自 YouTube 的代码就会过期,我们将被重定向到请求许可的 Google OAuth(例如:https://accounts.google.com/o/oauth2/auth?client_id=805j8tubb260venakqj8jq3f.6hl9e.并且每次按钮过期时我们都需要手动点击允许"按钮.

Basically my goal is to make a form that allows every visitor of my website to upload a video in the comment section to my OWN channel. Currently I'm using YouTube OAuth version 3 API for it. The problem is every 3600 seconds, the code from YouTube will be expired and we will be redirected to Google OAuth that asks for permission (Example: https://accounts.google.com/o/oauth2/auth?client_id=805j8tubb260venakqj8jq3f6hl9eluu.apps.googleusercontent.com). And we need to manually click the 'allow' button every time the button expires.

那么有没有可能只允许访问一次,然后我们就不需要再次拿代码给网站访问者上传权限?

So is it possible to allow access once, and then we dont need to take the code again to give the upload permission to the website visitor?

推荐答案

您应该按照 KENdi 关于如何获取令牌的说明进行操作——他几乎是直接从文档中提取的.但是,您应该只需要这样做一次.诀窍是将 refresh_token 保存在某处,然后在上传视频之前使用它来获取新的访问令牌.这是我快速而肮脏的 NodeJS 解决方案:

You should follow KENdi's instructions on how to get the token -- he pretty much pulled it straight from the docs. But, you should only have to do that once. The trick is to save the refresh_token somewhere, and then use that to get a new access token before you want to upload a video. Here's my quick and dirty NodeJS solution:

function getAccessToken () {
    const options = {
    method: 'POST',
    uri: 'https://accounts.google.com/o/oauth2/token',
    form: {
        client_id: 'YOUR_CLIENT_ID',
        client_secret: 'YOUR_CLIENT_SECRET',
        refresh_token: 'YOUR_REFRESH_TOKEN',
        grant_type: 'refresh_token'
      }
    }

    return request(options)
        .then(body => JSON.parse(body));
}

getAccessToken.then((token) => uploadSomeYoutubeVideo());

这篇关于用于检索令牌的 Youtube oAuth 2.0 API 永久代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆