【发布时间】:2011-07-29 14:00:03
【问题描述】:
我正在为我的研究开发一个 android 应用程序,并且我正在使用 OAuth(路标库)从 Web 服务中获取对用户数据的访问权限,这也是开发过程的一部分。我能够完成 OAuth 的常见步骤,并且我使用 Uri(用于回调应用程序),并且可以进入调用设备浏览器的步骤,选择验证我的应用程序,下一步是 SUPPOSED将浏览器重定向回应用程序....
相反,我收到一条错误消息,内容类似于“您没有打开权限:
appSchema://appName?authorizationSensitiveInfo..." '?' 之后的附属物是 oauth_token 和 oauth_verifier 来自 服务(我们可以假设所有步骤 直到重定向 “正确”)。
appSchema://appName 部分可能存在问题。据我了解,这是重定向 URL,它告诉 Uri 使用手机的浏览器来定位我的应用程序并调用 onResume() 方法。 appSchema://appName 的值来自哪里(在清单中定义?如果是在哪里?)。
为什么会出现权限问题?我必须为我的 Uri 设置访问我的应用程序的权限吗?我迷路了...如果您需要代码 sn-ps 来帮助我,请回复,我没有包含任何代码,因为这更像是我刚刚错过的一个概念...我现在不在我的机器上,但我如果这会使事情更容易理解,可以提供代码。真的是在这里打我的头......
为了回应一个很好的答案,我将如何处理我的简历
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
Log.d("StepGreenM", uri.toString());
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d("StepGreenM", verifier);
try {
provider.retrieveAccessToken(consumer, verifier);
TOKEN = consumer.getToken();
REQUEST_SECRET = consumer.getTokenSecret();
Log.d("StepGreenM", TOKEN);
Log.d("StepGreenM", REQUEST_SECRET);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
}
uri = getIntent().getData();
if (uri != null && CALLBACK_URI.getScheme().equals(uri.getScheme())) {
String token = settings.getString(HomeScreen.REQUEST_TOKEN, null);
String secret = settings.getString(HomeScreen.REQUEST_SECRET, null);
Intent i = new Intent(Intent.ACTION_VIEW); // Intent to go to the action view
try {
if(!(token == null || secret == null)) {
consumer.setTokenWithSecret(token, secret);
}
String otoken = uri.getQueryParameter(OAuth.OAUTH_TOKEN);
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
// We send out and save the request token, but the secret is not the same as the verifier
// Apparently, the verifier is decoded to get the secret, which is then compared - crafty
// This is a sanity check which should never fail - hence the assertion
Assert.assertEquals(otoken, consumer.getToken());
// This is the moment of truth - we could throw here
provider.retrieveAccessToken(consumer, verifier);
// Now we can retrieve the goodies
token = consumer.getToken();
secret = consumer.getTokenSecret();
//Save it to a settings file
HomeScreen.saveAuthInformation(settings, token, secret);
// Clear the request stuff, now that we have the real thing
HomeScreen.saveRequestInformation(settings, null, null);
i.putExtra(USER_TOKEN, token);
i.putExtra(CONSUMER_SECRET, secret);
//GO TO APPLICATION
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
} finally {
startActivity(i); // we either authenticated and have the extras or not, but are going to the action view
this.setContentView(R.layout.indivaction);
finish();
}
}
}
不确定是什么真正让这个崩溃......但就像我说的那样,当调用这个方法时它会强制关闭。我知道它可以通过重定向,因为我使用 httpSniffer 检查进出服务器的消息...
【问题讨论】:
标签: android oauth callback android-manifest signpost