2011-01-10 13 views
5

Negli ultimi 5 giorni sono alla ricerca di un miglior pice di codice di lavoro per twitter in Android usando OAuth ... Ho trovato molto. Ma non un solo 1 funziona perfettamente. Qualsiasi come ottengo del codice. Funziona ma ho un problema. Mi chiedo cosa devo scrivere in String callBack = "?" in modo che il mio browser Android mi reindirizzasse verso la mia applicazione invece di rimanere lì dopo l'autenticazione .. Se non sto usando un CallBack e uso OAuth.OUT_OF_BAND allora il browser mostra un codice pin e non reindirizza il browser alla mia applicazione. Per favore aiutami a farmi sapere cosa sto sbagliando.Come posso richiamare in Android usando OAuth per Twitter?

Ecco il mio codice va

package com.example.tweeter; 

import oauth.signpost.OAuth; 
import oauth.signpost.OAuthProvider; 
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; 
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; 
import oauth.signpost.exception.OAuthCommunicationException; 
import oauth.signpost.exception.OAuthExpectationFailedException; 
import oauth.signpost.exception.OAuthMessageSignerException; 
import oauth.signpost.exception.OAuthNotAuthorizedException; 

import org.apache.http.client.HttpClient; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 

public class Tweeter extends Activity { 

    private String CONSUMER_KEY = "CONSUMER_KEY"; 
    private String CONSUMER_SECRET = "CONSUMER_SECRET"; 
    private static final Uri CALLBACK_URI = Uri.parse("PicPuzzle://tkxel"); 
    private String CALLBACK_URL = "PicPuzzle://tkxel"; 
    OAuthProvider provider ; 
    CommonsHttpOAuthConsumer consumer ; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     consumer = new CommonsHttpOAuthConsumer(
       CONSUMER_KEY, CONSUMER_SECRET); 

     provider = new CommonsHttpOAuthProvider(
       "http://twitter.com/oauth/request_token", 
       "http://twitter.com/oauth/access_token", 
       "http://twitter.com/oauth/authorize"); 
     provider.setOAuth10a(true); 

     HttpClient client = new DefaultHttpClient(); 

     String authUrl = "http://www.yahoo.com"; 
     try { 

      //This line work perfect but it not redirect me to my application 
      authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND); 

      //I want to send a calll back but It give exception 
      ///*** authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URI.toString()); 

     } catch (OAuthMessageSignerException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (OAuthNotAuthorizedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (OAuthExpectationFailedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (OAuthCommunicationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); 
    } 

    @Override 
    protected void onResume() { 
     // this must be places in activity#onResume() 
     Uri uri = this.getIntent().getData(); 
     if (uri != null && uri.toString().startsWith(CALLBACK_URL)) { 
      String verifier = uri.getQueryParameter("oauth_verifier"); 
      // this will populate token and token_secret in consumer 
      try { 
       provider.retrieveAccessToken(consumer, verifier); 
      } catch (OAuthMessageSignerException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (OAuthNotAuthorizedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (OAuthExpectationFailedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (OAuthCommunicationException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     super.onResume(); 
    } 
} 

E il mio AndroidManifest.xml aspetto come questo

<uses-permission 
    android:name="android.permission.INTERNET"/> 
<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
> 
    <activity 
     android:name=".Tweeter" 
     android:label="@string/app_name" 
    > 
     <intent-filter> 
      <action 
       android:name="android.intent.action.MAIN"/> 
      <category 
       android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 
    <intent-filter> 
     <action 
      android:name="android.intent.action.VIEW" 
     ></action> 
     <category 
      android:name="android.intent.category.DEFAULT" 
     ></category> 
     <category 
      android:name="android.intent.category.BROWSABLE" 
     ></category> 
     <data 
      android:scheme="PicPuzzle" 
      android:host="tkxel" 
     ></data> 
    </intent-filter> 
</application> 
<uses-sdk 
    android:minSdkVersion="7"/>`enter code here` 

risposta

7

L'URL di callback dovrebbe essere basata su ciò che si configura per la vostra attività nel manifesto. Sembra che tu stia usando scheme="PicPuzzle", host="tkxel". Così il vostro URL callback è PicPuzzle://tkxel

penso che il tag <data> dovrebbe essere sulla particolare Activity si desidera ricevere la richiamata però (sembra lo avete sulla intera applicazione al momento).

+0

Ci provo ma ancora nessuna soluzione ...! Mostra sempre l'errore oauth.signpost.exception.OAuthNotAuthorizedException: autorizzazione non riuscita (il server ha risposto con un 401). Questo può accadere se la chiave del consumatore non è corretta o le firme non corrispondono. 01-10 16: 53: 20.614: WARN/System.err (5447): su oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse (AbstractOAuthProvider.java:239) 01-10 16: 53: 20.614: WARN/System.err (5447): a oauth.signpost.AbstractOAuthProvider.retrieveToken (AbstractOAuthProvider.java:189) – Arslan

+0

Ho dato un'occhiata più da vicino al codice. Vedo un paio di potenziali problemi: - Firmatario del messaggio: impostato con "consumer.setMessageSigner (new HmacSha1MessageSigner());" - recupero del token di accesso. Usa "OAuth.OAUTH_VERIFIER" per il verificatore (anche se probabilmente è la stessa cosa: – ShibbyUK

+0

Grazie per considerazione ... Ma dove inserire questo codice? Firmatario del messaggio: Imposta con "consumer.setMessageSigner (new HmacSha1MessageSigner());" – Arslan

Problemi correlati