我的安卓应用程序使用Java OAuth库,在Twitter上找到了授权的here。我可以得到一个请求令牌,授权令牌和确认,但是当浏览器尝试回调url来重新连接我的应用程序时,它不会使用我在代码中提供的URL,而是使用我在Twitter注册时提供的URL。
注意:
你知道我做错了什么吗?
相关代码:
public class FirstActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OAuthAccessor client = defaultClient();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(client.consumer.serviceProvider.userAuthorizationURL + "?oauth_token="
+ client.requestToken + "&oauth_callback=" + client.consumer.callbackURL));
startActivity(i);
}
OAuthServiceProvider defaultProvider()
{
return new OAuthServiceProvider(GeneralRuntimeConstants.request_token_URL,
GeneralRuntimeConstants.authorize_url, GeneralRuntimeConstants.access_token_url);
}
OAuthAccessor defaultClient()
{
String callbackUrl = "myapp:///";
OAuthServiceProvider provider = defaultProvider();
OAuthConsumer consumer = new OAuthConsumer(callbackUrl,
GeneralRuntimeConstants.consumer_key, GeneralRuntimeConstants.consumer_secret,
provider);
OAuthAccessor accessor = new OAuthAccessor(consumer);
OAuthClient client = new OAuthClient(new HttpClient4());
try
{
client.getRequestToken(accessor);
} catch (Exception e)
{
e.printStackTrace();
}
return accessor;
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null)
{
String access_token = uri.getQueryParameter("oauth_token");
}
}
}
// Manifest file
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FirstActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp"/>
</intent-filter>
</activity>
</application>发布于 2010-03-08 20:11:40
推特不接受OAuth请求(Twitter API Announce)中请求的回调,只会重定向到应用程序设置中指定的回调URL (请注意,"localhost“是不允许的)。
我假设你检查了Oauth-callback-on-android问题。
Android猜想--
读了一下,我发现Android浏览器将MyApp:/重定向到你的应用程序,我猜Twitter不喜欢这个定制的URI前缀。我不是安卓开发人员,但我可能会提出一个建议,那就是把"www.myapp.com“放到网上,然后重定向到那里。
因此,让OAuth返回到http://www.myapp.com/redirect.aspx?oauth_token=abc,并将页面重定向到myapp:///oauth_token=... (期望结果)
发布于 2011-08-23 21:55:26
在我的例子中,我做到了这一点:
String authURL = m_provider.retrieveRequestToken (m_consumer, CALLBACK_URL);在清单中:
<activity android:configChanges = "keyboardHidden|orientation" android:name = "xxxx.android.xxxxx">
<intent-filter>
<action android:name = "android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="tweet" />
</intent-filter>在这种情况下,回调url将是: myapp://tweet
发布于 2010-02-04 23:56:59
在我看来,你做的是正确的事情,而Twitter总是接受你注册的回调URL,这就搞砸了。有没有办法更改已注册的URL?也许你可以重新注册,下次尝试Android回调,看看会发生什么。
https://stackoverflow.com/questions/2199357
复制相似问题