问题描述
我想知道是否有可能在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和机密的信息,我尝试尝试获取访问令牌. (MessageBox显示"错误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/OAuth2This 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流时请求刷新"令牌.这就是通过在代码中将"duration"作为"permanent"发送的方式.刷新令牌可用于自动检索新的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快速入门" Wiki页面,但我将在这里进行总结:
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:
- 创建OAuth客户端(在 https://www.reddit.com/prefs/apps)类型为脚本"
- 使用POST参数
grant_type=password&username=<USERNAME>&password=<PASSWORD>
向https://www.reddit.com/api/v1/access_token
发出请求.发送您的客户端ID和密码作为HTTP基本身份验证.<USERNAME>
必须注册为您发送的OAuth 2客户端ID的开发者.
- Create an OAuth client (under https://www.reddit.com/prefs/apps) with type = "script"
- Make a request to
https://www.reddit.com/api/v1/access_token
with POST parametersgrant_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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!