2010-02-04 57 views
7

我的Android应用程序使用Java OAuth库,在Twitter上找到here进行授权。我能够获得请求令牌,授权令牌并获得确认,但是当浏览器尝试回拨url以重新与我的应用程序连接时,它不会使用我在代码中提供的URL,而是使用我在注册时提供的URL与Twitter。Android上的OAuth + Twitter:回拨失败

注:
1.当我的注册与Twitter的应用程序,我提供一个假想的回调网址:http://abz.xyc.com和设置应用程序类型的浏览器。
2.我在我的代码“myapp”中提供了一个回调网址,并为我的活动添加了一个意向过滤器,将Browsable类别和数据方案添加为“myapp”。
3.授权时调用的URL确实包含te代码中指定的回调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> 

回答

11

Twitter不遵守OAuth请求(Twitter API Announce)中请求的回调,并且只会重定向到应用程序设置中指定的回调URL(请注意,不允许使用“localhost”)。

我假设你检查了Oauth-callback-on-android的问题。

的Android guesswork--
一点阅读后,我看到的Android浏览器重定向MyApp的:///您的应用程序和我猜的Twitter不喜欢这个定制的URI前缀。我不是Android开发者,但我可能会提出的一个建议是在网上获得“www.myapp.com”,并在那里重新导向。

因此,有您的OAuth返回http://www.myapp.com/redirect.aspx?oauth_token=abc和有页面重定向到myapp:///oauth_token=...(期望的结果)

+0

感谢您的回复。 1.能否请你举你的语句引用: “推特不认可的OAuth请求,请求回调,只会重定向到应用程序设置指定的回调URL(注意,‘本地主机’是不允许的)。“ 2.是的,我做了检查OAuth的回调上Android的问题,并且已经设置我的应用程序类型(即浏览器),因此,再次 感谢。 – Samuh 2010-03-08 13:35:04

+0

添加链接的Twitter API公布。 – 2010-03-08 13:47:34

+0

如果在回调要求的OAuth请求没有兑现,那么我怎么称呼我的应用程序后授权?Twitter不接受Android风格的URL,这些需要被覆盖。如果上述不工作有什么替代方法? 请帮助。 [你得到10现在点; 125回答完这个最后一个问题后:P] – Samuh 2010-03-09 09:52:52

0

它看起来像你对我在做正确的事和Twitter被永远接受你注册的回调URL搞砸了。有什么方法可以更改注册的网址吗?也许你可以重新注册并下次尝试Android回调,看看会发生什么。

+0

是的,我们可以通过点击编辑应用程序设置按钮来更改注册的URL。我尝试了两种不同的URL,但没有多少运气,可能我会重新注册并查看会发生什么。 – Samuh 2010-02-05 03:17:36

+0

我没有任何运气重新注册我的应用程序,但... – Samuh 2010-02-15 12:28:40

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://鸣叫

+0

你会告诉我什么是myapp这个应用程序的名称和什么来代替推文 – 2014-01-24 07:11:35

0

我的问题是,我试图用我制作Twitter应用程序的相同帐户登录。在我用我的个人资料登录后,回拨工作(到目前为止)。