有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

servlets使用Scribe OAuth Java库进行Google API授权

我正在尝试创建一个Java类,该类将调用Google的API来重新创建一个访问令牌,以访问具有新权限/更大范围的用户帐户。这个类将由我创建的Javaservlet实例化。我希望该类中有一个函数返回一个新的访问令牌。为了让这个类实现这一点,我使用了Scribe

在Scribe的quick guide中,有两个步骤与我有关,让我感到困惑:

Step Three: Making the user validate your request token

Let’s help your users authorize your app to do the OAuth calls. For this you need to redirect them to the following URL:

String authUrl = service.getAuthorizationUrl(requestToken);

After this either the user will get a verifier code (if this is an OOB request) or you’ll receive a redirect from Twitter with the verifier and the requestToken on it (if you provided a callbackUrl)

Step Four: Get the access Token

Now that you have (somehow) the verifier, you need to exchange your requestToken and verifier for an accessToken which is the one used to sign requests.

Verifier v = new Verifier("verifier you got from the user");
Token accessToken = service.getAccessToken(requestToken, v); // the requestToken you had from step 2

它似乎没有指定如何从用户处获取verifier。我该怎么做?如何将我的用户重定向到authURL,以及如何让它将其verifier发送回我的这个类,它首先启动了请求

如果这还不清楚,让我用不同的结构来回答这个问题,把Scribe从等式中去掉:从Google获得授权码(然后将用于获取刷新令牌和访问令牌),我将在servlet中执行以下URL连接(是的,我尝试在没有Scribe库的情况下回答这个问题,但仍然无法解决):

URL authURL = new URL("https://accounts.google.com/o/oauth2/auth");
HttpsURLConnection authCon = (HttpsURLConnection) authURL.openConnection();
authCon.setRequestMethod("GET");
authCon.setDoOutput(false);
authCon.setConnectTimeout(100000);
authCon.setRequestProperty("response_type", "code");
authCon.setRequestProperty("client_id", CLIENT_ID);
authCon.setRequestProperty("redirect_uri",
        "http://**************.com/parseAuth/");
authCon.setRequestProperty("scope", convertToCommaDelimited(scopes));
authCon.setRequestProperty("state", csrfSec);
authCon.setRequestProperty("access_type", "offline");
authCon.setRequestProperty("approval_prompt", "auto");
authCon.setRequestProperty("include_granted_scopes", "true");

让我陷入困境的是我应该为redirect URI投什么。在获得用户对新作用域的批准后,此授权URL将向redirect URI返回授权代码,而对调用它的内容似乎什么也没有。(我说的对吗?)因此,如果我有另一个servlet作为redirect URI来解析/提取响应中的授权代码,我该如何将该授权代码返回到我的第一个初始servlet在我看来,似乎没有办法让它将值返回给servlet,返回的位置与调用URL的代码的位置相同。看起来函数必须到此结束,所有新操作都必须在新servlet中进行。但是如果是这样的话,我将该验证代码发送到Google的API,该API将向另一个servlet发回一个刷新令牌和访问令牌,我将使它成为它的redirect URI,那么我如何可能将该信息返回到最初的servlet呢?这似乎是同一个问题的核心,与我的问题与抄写

我已经在这个问题上纠缠了好几个小时,似乎不知道该怎么做我觉得我遗漏了一些关键概念、要素或步骤我需要澄清这一点。如果相关的话,我的servlet托管在OpenShift上的Jboss应用服务器上


共 (0) 个答案