【发布时间】:2015-11-06 23:31:27
【问题描述】:
我正在尝试让已安装的应用程序在 Reddit 的 api 上与 Oauth2 一起使用。我正在使用 Windows 运行时 api 的 httpclient 发出请求,并使用 webauthenticationbroker 获取代码来执行 GET 请求以接收令牌。我使用这个请求了一个令牌:
https://www.reddit.com/api/v1/authorize?client_id=" + client_id + "&response_type=code&state=" + "testing" + "&redirect_uri=http://abcd.com&duration=" + "permanent" + "&scope=" + "vote,identity"
得到了一个代码,所以我使用了 POST(内容类型为:application/x-www-form-urlencoded):
https://www.reddit.com/api/v1/access_token
身体是这样的:
grant_type=https://oauth.reddit.com/grants/installed_client&\
device_id="+id + "&code=" + code
(code和id是第一步收到的code,id是生成的UUID)
然后我得到了类似的东西:
{"access_token": "--5e65dP1dI_1vgLbqvi7zRB6cnU", "token_type": "bearer", "expires_in": 3600, "scope": "*"}
所以我提取了令牌并得到了这个:
--5e65dP1dI_1vgLbqvi7zRB6cnU
然后我尝试使用这些标头在https://oauth.reddit.com/api/v1/me 上执行 GET 请求:
{
User-Agent: (testUWP client by /u/bored_reddit_user)
Authorization: bearer --5e65dP1dI_1vgLbqvi7zRB6cnU
}
我用状态码 403 原因短语禁止返回这些标题:
{
Connection: keep-alive
Server: cloudflare-nginx
Strict-Transport-Security: max-age=15552000; includeSubDomains; preload
Transfer-Encoding: chunked
cache-control: max-age=0, must-revalidate
x-ua-compatible: IE=edge
CF-RAY: 23f5127a6a2911a1-SJC
Date: Tue, 03 Nov 2015 03:42:58 GMT
x-frame-options: SAMEORIGIN
access-control-allow-origin: *
X-Moose: majestic
x-reddit-tracking: https://pixel.redditmedia.com/pixel/of_destiny.png?v=BZoi0ikdGrSYn9U9xM6GWeYcRRb0W50fSQuGYb1Q8Oe7E5WVB6qTA4hRqlx9vDfpLOKzpE3Z5Wo%3D
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
access-control-expose-headers: X-Reddit-Tracking, X-Moose
}{
Content-Type: application/json; charset=UTF-8
}
还有这个内容:
{"explanation": "Please log in to do that.", "reason": "USER_REQUIRED"}
我不知道自己做错了什么,谁能帮帮我?
【问题讨论】:
-
使用
installed_client授权类型的原因是什么?它仅适用于在没有用户上下文的情况下发出请求 - https://oauth.reddit.com/api/v1/me 似乎需要用户上下文才能返回当前经过身份验证的用户。您是否尝试过使用authorization_code作为授权类型? -
感谢@ZanyCadence 的工作。是否有任何页面解释每种授权类型?我会认为
installed_client类型对于将安装在用户计算机上的客户端是有意义的......
标签: api oauth oauth-2.0 dotnet-httpclient c++-cx