是否可以在没有重定向服务器的情况下使用 OAuth 2.0? [英] Is it possible to use OAuth 2.0 without a redirect server?

查看:43
本文介绍了是否可以在没有重定向服务器的情况下使用 OAuth 2.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个与 SurveyMonkey API 交互的基于 Java 的本地客户端.

I'm trying to create a local Java-based client that interacts with the SurveyMonkey API.

SurveyMonkey 需要使用 OAuth 2.0 的长期访问令牌,我对此不太熟悉.

SurveyMonkey requires a long-lived access token using OAuth 2.0, which I'm not very familiar with.

我已经在谷歌上搜索了几个小时,我认为答案是否定的,但我只想确定:

I've been googling this for hours, and I think the answer is no, but I just want to be sure:

我是否可以编写一个与 SurveyMonkey 交互的简单 Java 客户端,而无需在某些云中设置自己的重定向服务器?

Is it possible for me to write a simple Java client that interacts with the SurveyMonkey, without setting up my own redirect server in some cloud?

我觉得必须拥有自己的在线服务才能接收 OAuth 2.0 生成的不记名令牌.我是否可能无法让 SurveyMonkey 直接向我的客户发送不记名令牌?

I feel like having my own online service is mandatory to be able to receive the bearer tokens generated by OAuth 2.0. Is it possible that I can't have SurveyMonkey send bearer tokens directly to my client?

如果我要在某处设置自己的自定义 Servlet,并将其用作 redirect_uri,那么正确的流程如下:

And if I were to set up my own custom Servlet somewhere, and use it as a redirect_uri, then the correct flow would be as follows:

  1. Java 客户端从 SurveyMonkey 请求不记名令牌,使用redirect_uri 是我自己的自定义 servlet URL.
  2. SurveyMonkey 将令牌发送到我的自定义 servlet URL.
  3. Java 客户端轮询自定义 servlet URL 直到令牌可用?

这是正确的吗?

推荐答案

不完全是,OAuth 流程的全部意义在于用户(您代表您访问数据的客户端)需要授予您权限访问他们的数据.

Not exactly, the whole point of the OAuth flow is that the user (the client you're accessing the data on behalf of) needs to give you permission to access their data.

请参阅身份验证说明.您需要将用户发送到 OAuth 授权页面:

See the authentication instructions. You need to send the user to the OAuth authorize page:

https://api.surveymonkey.net/oauth/authorize?api_key<your_key>&client_id=<your_client_id>&response_type=code&redirect_uri=<your_redirect_uri>

这将向用户显示一个页面,告诉他们您请求访问他们帐户的哪些部分(例如查看他们的调查、查看他们的回复等).一旦用户通过单击该页面上的授权"批准该操作,SurveyMonkey 将自动转到您设置为重定向 URI 的任何内容(确保上述 url 中的那个与您在应用程序设置中设置的内容匹配)和代码.

This will show a page to the user telling them which parts of their account you are requesting access to (ex. see their surveys, see their responses, etc). Once the user approves that by clicking "Authorize" on that page, SurveyMonkey will automatically go to whatever you set as your redirect URI (make sure the one from the url above matches with what you set in the settings for your app) with the code.

因此,如果您的重定向 URL 是 https://example.com/surveymonkey/oauth,SurveyMonkey 会使用代码将用户重定向到该 URL:

So if your redirect URL was https://example.com/surveymonkey/oauth, SurveyMonkey will redirect the user to that URL with a code:

https://example.com/surveymonkey/oauth?code=

您需要获取该代码,然后通过向 https://api.surveymonkey.net/oauth/token?api_key=<your_api_key> 发出 POST 请求来将其交换为访问令牌使用以下帖子参数:

You need to take that code and then exchange it for an access token by doing a POST request to https://api.surveymonkey.net/oauth/token?api_key=<your_api_key> with the following post params:

client_secret=<your_secret>
code=<auth_code_you_just_got>
redirect_uri=<same_redirect_uri_as_before>
grant_type=authorization_code

这将返回一个访问令牌,然后您可以使用该访问令牌访问用户帐户上的数据.您不会将访问令牌提供给您用来访问用户帐户的用户.不需要投票或任何东西.

This will return an access token, you can then use that access token to access data on the user's account. You don't give the access token to the user it's for you to use to access the user's account. No need for polling or anything.

如果您只是访问自己的帐户,则可以使用应用设置页面中提供的访问令牌.否则,如果不设置自己的重定向服务器,就无法为用户获取访问令牌(除非所有用户都与您在同一组中,即同一帐户下的多个用户;但我不会涉及).一旦用户授权,SurveyMonkey 需要一个地方向您发送代码,您不能只请求一个.

If you're just accessing your own account, you can use the access token provided in the settings page of your app. Otherwise there's no way to get an access token for a user without setting up your own redirect server (unless all the users are in the same group as you, i.e. multiple users under the same account; but I won't get into that). SurveyMonkey needs a place to send you the code once the user authorizes, you can't just request one.

这篇关于是否可以在没有重定向服务器的情况下使用 OAuth 2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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