在Android上使用Google API Java客户端,POST请求似乎不会使用OAuth通过Google App Engine应用进行身份验证

问题描述 投票:3回答:1

我有一个Android客户端需要使用OAuth使用python Google App Engine应用进行身份验证。 我跟着这篇文章

并且能够使用HTTP Get Request成功完成此操作。 Android客户端使用com.google.api.client包来完成此任务:

OAuthHmacSigner _signer;
HttpTransport TRANSPORT = new NetHttpTransport();
_signer = new OAuthHmacSigner();
_signer.clientSharedSecret = CONSUMER_SECRET;

// STEP1: get a request token
OAuthGetTemporaryToken requestToken = new OAuthGetTemporaryToken(REQUEST_TOKEN_URL);
requestToken.consumerKey = CONSUMER_KEY;
requestToken.transport = TRANSPORT;
requestToken.signer = _signer;
requestToken.callback = "http://my_app_specific_callback";
requestTokenResponse = requestToken.execute();
OAuthAuthorizeTemporaryTokenUrl authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(AUTHORIZE_URL);
authorizeUrl.temporaryToken = requestTokenResponse.token;
// at this point, redirect the user using a WebView to the URL string returned by authorizeUrl.build().  Continue below once the user has granted request.

// STEP2: get an access token
_signer.tokenSharedSecret = requestTokenResponse.tokenSecret;
OAuthGetAccessToken accessToken = new OAuthGetAccessToken(ACCESS_TOKEN_URL);
accessToken.consumerKey = CONSUMER_KEY;
accessToken.signer = _signer;
accessToken.transport = TRANSPORT;
accessToken.temporaryToken = requestTokenResponse.token;
accessTokenResponse = accessToken.execute();

// STEP3: use the access token acquired above to access a protected resource
_signer.tokenSharedSecret = accessTokenResponse.tokenSecret;
OAuthParameters parameters = new OAuthParameters();
parameters.consumerKey = CONSUMER_KEY;
parameters.token = accessTokenResponse.token;
parameters.signer = _signer;
HttpRequestFactory factory = TRANSPORT.createRequestFactory(parameters);
HttpRequest req = factory.buildGetRequest(new GenericUrl(PROTECTED_URL_GET_USER_EMAIL));
com.google.api.client.http.HttpResponse resp = req.execute();

在上面的代码片段中,所有3个步骤都可以正常工作。 在我的Google App Engine服务器上,我检查了对PROTECTED_URL_GET_USER_EMAIL的GET请求,它包含一个正确的身份验证HTTP标头:

'Authorization': 'OAuth oauth_consumer_key="shiprack-test1.appspot.com", oauth_nonce="...", oauth_signature="...", oauth_signature_method="HMAC-SHA1", oauth_timestamp="...", oauth_token="..."'

使用GAE上的oauth python包(google.appengine.api.oauth),我的服务器能够对用户进行身份验证并确定用户的电子邮件地址(oauth.get_current_user())。

但是,问题是当我将PROTECTED_URL_GET_USER_EMAIL转换为HTTP Post请求时。 这就是我这样做的方式:

Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("param1", "value1")
paramsMap.put("param2", "value2")
HttpRequest req = _httpRequestFactory.buildPostRequest(new GenericUrl(url), new UrlEncodedContent(paramsMap));
req.setFollowRedirects(true);
com.google.api.client.http.HttpResponse resp = req.execute();

但现在,在GAE python服务器端,我无法确定当前用户。 HTTP标头包含相同的OAuth身份验证标头(具有不同的随机数,时间戳和签名,但具有相同的oauth标记)。 “param1”和“param2”位于HTTP Payload中。 也许我的POST请求构造不正确?

我使用Ikai Lan (Google App Engine支持团队)代码为python客户端对我的GAE服务器进行身份验证。 这也有效......原始客户端有GET请求,甚至当我修改它以使用POST请求时。 我注意到,尽管使用POST请求,oauth参数作为URL编码值包含在HTTP有效负载中而不是HTTP头中。 这是Oauth签名的HTTP帖子请求的要求吗?

提前致谢!

android google-app-engine oauth google-api google-api-java-client
1个回答
1
投票

遗憾的是,尚未实现基于表单编码的HTTP内容参数的正确OAuth 1.0a编码。 我们收到了不少要求。 已经有一个功能请求已经打开。

© www.soinside.com 2019 - 2024. All rights reserved.