如何获取访问令牌?(Reddit API) [英] How to get access token? (Reddit API)

查看:36
本文介绍了如何获取访问令牌?(Reddit API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在 Reddit 上获得供个人使用的永久访问令牌?只有我会使用该应用程序.

I wonder if it is possible to get a permanent access token for personal use on Reddit? It will only be me using the App.

对于用户,访问令牌会在 1 小时后过期.

For users, the access token expires after 1 hour.

我使用以下有关我的客户端 ID 和机密的信息,开始尝试获取访问令牌.(消息框显示错误 401")

My using the below information that I have about my client-id and secret, I put up a start attempt of trying to get an access token. (MessageBox show "Error 401")

如果用户将获得令牌,则必须在浏览器中单击允许".这里描述得很好.https://github.com/reddit/reddit/wiki/OAuth2这不是我所追求的.我只通过代码寻求个人使用的访问令牌.这可能吗?

If a user will get a token, one have to click "Allow" in the browser. Very well described here. https://github.com/reddit/reddit/wiki/OAuth2 This it NOT what I am after. I am after for, personal use, an access token only through code. Is this possible?

            String requestUrl = "https://ssl.reddit.com/api/v1/access_token";

        RestSharp.RestClient rc = new RestSharp.RestClient();
        RestSharp.RestRequest request = new RestSharp.RestRequest(requestUrl, RestSharp.Method.POST);
        request.AddHeader("Content-Type", "application/json");
        //request.AddHeader("Authorization", ""); //???
        request.AddHeader("x-li-format", "json");

        request.AddParameter("client_id", "abcdefg");
        request.AddParameter("client_secret", "abc123-456");
        request.AddParameter("grant_type", "abc123-456");
        request.AddParameter("scope", "identity");
        request.AddParameter("state", "adhasegw"); //whatever value
        request.AddParameter("duration", "permanent");
        request.AddParameter("redirect_uri", "http://mywebsite.co");

        request.RequestFormat = RestSharp.DataFormat.Json;

        RestSharp.RestResponse restResponse = (RestSharp.RestResponse)rc.Execute(request);
        RestSharp.ResponseStatus responseStatus = restResponse.ResponseStatus;



        MessageBox.Show(restResponse.Content.ToString() + "," + responseStatus.ToString());

推荐答案

截至目前,您无法检索永久访问令牌.您有 2 个接近的选项.

As of right now, you cannot retrieve a permanent access token. You have 2 options that come close.

首先是在使用标准 OAuth 流程时请求刷新"令牌.这就是您通过在代码中将持续时间"发送为永久"来执行的操作.刷新令牌可用于在无需用户干预的情况下自动检索新的 1 小时访问令牌;唯一的手动步骤是初始检索刷新令牌.

The first is to request a "refresh" token when using the standard OAuth flow. That's what you're doing by sending "duration" as "permanent" in your code. The refresh token can be used to automatically retrieve new 1 hour access tokens without user intervention; the only manual steps are on the initial retrieval of the refresh token.

第二种选择,仅适用于编写个人使用的脚本时,使用 password 授权类型.这些步骤在 reddit 的 OAuth 快速入门"维基页面上有更详细的描述,但我会在这里总结:

The second alternative, which applies only when writing a script for personal use, is to use the password grant type. The steps are described in more detail on reddit's "OAuth Quick Start" wiki page, but I'll summarize here:

  1. 创建 OAuth 客户端(在 https://www.reddit.com/prefs/apps 下) with type = "script"
  2. 使用 POST 参数向 https://www.reddit.com/api/v1/access_token 发出请求 grant_type=password&username=<USERNAME>&password=<密码>.将您的客户端 ID 和机密作为 HTTP 基本身份验证发送. 必须注册为您发送的 OAuth 2 客户端 ID 的开发者.
  1. Create an OAuth client (under https://www.reddit.com/prefs/apps) with type = "script"
  2. Make a request to https://www.reddit.com/api/v1/access_token with POST parameters grant_type=password&username=<USERNAME>&password=<PASSWORD>. Send your client ID and secret as HTTP basic authentication. <USERNAME> must be registered as a developer of the OAuth 2 client ID you send.

这篇关于如何获取访问令牌?(Reddit API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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