我的Android应用程序使用Java OAuth库,在Twitter上找到here进行授权。我能够获取请求令牌,授权令牌并获得确认但是当浏览器尝试回调URL以重新连接我的应用程序时,它不使用我在代码中提供的URL,而是使用我在注册时提供的URL用推特。
注意:
1.在使用Twitter注册我的应用程序时,我提供了一个假设的回调网址:http://abz.xyc.com并将应用程序类型设置为浏览器。
2.我在我的代码“myapp”中提供了一个回调网址,并为我的活动添加了一个意图过滤器,其中Browsable类别和数据方案为“myapp”。
3.授权时调用的URL确实包含我在代码中指定的回调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>
答案 0 :(得分:11)
Twitter不支持OAuth请求(Twitter API Announce)中请求的回调,并且只会重定向到“应用程序设置”中指定的回调URL(请注意,不允许使用“localhost”)。
我假设您检查了Oauth-callback-on-android个问题。
Android猜测 -
经过一段时间的阅读后,我看到Android浏览器将MyApp:///重定向到您的应用程序,我猜测Twitter不喜欢这个定制的URI前缀。我不是Android开发人员,但我可能会提出一个建议是在网络上获取“www.myapp.com”并重新定位。
让你的OAuth返回http://www.myapp.com/redirect.aspx?oauth_token=abc
并将该页面重定向到myapp:///oauth_token=...
(所需的结果)
答案 1 :(得分:3)
在我的情况下,我有这个工作:
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>
在这种情况下,回调网址将是:myapp:// tweet
答案 2 :(得分:0)
在我看来,你正在做正确的事情,Twitter总是接受你注册的回调网址而搞砸了。有没有办法更改注册的URL?也许你下次可以重新注册并尝试Android回调,看看会发生什么。
答案 3 :(得分:0)
我的问题是我尝试使用我制作Twitter应用程序的同一帐户登录。在我使用我的个人资料登录后,回拨工作(到目前为止)。