Question

Je suis en train d'intégrer Twitter à mon application, mais je ne peux pas sembler le faire au travail.

Ceci est mon code:

public class OAuthForTwitter extends Activity {

    private CommonsHttpOAuthConsumer httpOauthConsumer;
    private OAuthProvider httpOauthprovider;
    public final static String consumerKey = "{removed}";
    public final static String consumerSecret = "{removed}";
    private final String CALLBACKURL = "sosInternational:///HierBenIkNu";
    private Twitter twitter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        doOAuth();
    }

    /**
     * Opens the browser using signpost jar with application specific
     * consumerkey and consumerSecret.
     */

    private void doOAuth() {
        try {
            httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
            httpOauthprovider = new DefaultOAuthProvider(
                    "http://twitter.com/oauth/request_token",
                    "http://twitter.com/oauth/access_token",
                    "http://twitter.com/oauth/authorize");
            String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL);
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        Uri uri = intent.getData();
        if (uri != null && uri.toString().startsWith(CALLBACKURL)) {

            String verifier = uri
                    .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);

            try {
                // this will populate token and token_secret in consumer

                httpOauthprovider.retrieveAccessToken(httpOauthConsumer,
                        verifier);

                // TODO: you might want to store token and token_secret in you
                // app settings!!!!!!!!

                AccessToken a = new AccessToken(httpOauthConsumer.getToken(),
                        httpOauthConsumer.getTokenSecret());

                // initialize Twitter4J

                twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(consumerKey, consumerSecret);
                twitter.setOAuthAccessToken(a);

                // create a tweet

                Date d = new Date(System.currentTimeMillis());
                String tweet = "#OAuth working! " + d.toLocaleString();

                // send the tweet

                twitter.updateStatus(tweet);

            } catch (Exception e) {

                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }
    }
}

Quand je suis authentifiant fait sur le site de Twitter, il devrait me rediriger vers l'application.

Mais au lieu, je reçois cette page introuvable:

text alt

J'ai dans mon AndroidManifest:

<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="sosInternational" android:host="HierBenIkNu"></data>  
    </intent-filter>  

Comment puis-je revenir à mon application avec les touches que je revienne?

Était-ce utile?

La solution

Ok, il était tout à fait une erreur stupide.

Mon <intent-filter> n'a pas été dans une application ..

Voici comment il est maintenant:

<activity 
        android:name=".OAuthForTwitter"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden"
        android:launchMode="singleInstance">
        <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="sosInternational" android:host="OAuthForTwitter"></data>  
        </intent-filter>
    </activity>

Ce genre de travaux, il charge simplement l'application tout du début.

est pas là un moyen de simplement « revenir » à la dernière activité sans redémarrer l'application entière?

Autres conseils

J'ai résolu ce. Pas exactement la façon dont vous avez développé, mais une façon légère différente. Voici les étapes décrivant ce que je l'ai fait.

  1. Utilisez WebView au lieu de l'ouvrir dans le navigateur Web. L'un des principaux avantages de le faire est, vous pouvez suivre les redirections d'URL

  2. méthode

    appel setWebViewClient de votre méthode WebView et remplacement shouldOverrideUrlLoading de votre classe, je l'ai utilisé classe interne.

  3. Vous avez paramètre url dans votre méthode. Vérifiez si elle commence par votre propre URL de retour d'appel ou non,. (Note: Cette URL contient l'utilisateur jeton et secret de l'utilisateur qui est nécessaire pour l'autorisation)

  4. Après avoir terminé votre tâche, vous pouvez masquer l'activité ou supprimer WebView ou quelque chose que vous désirez.

EDIT : Ceci est la façon dont OAuth habituellement utilisé dans l'application web, donc nous ne avons pas besoin moyen XAUTH. (En cas d'autres membres de la communauté ne savent pas)

Hope il vous aidera.

Votre URL de rappel doit être "sosInternational: // HierBenIkNu" (au lieu de "sosInternational: /// HierBenIkNu"). Dans le code Java

private final String CALLBACKURL = "sosInternational://HierBenIkNu";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top