Twitter是不是重定向到回调URL的Andr​​oid应用程序 [英] twitter is not redirecting to the callback url in android app

查看:778
本文介绍了Twitter是不是重定向到回调URL的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用twitter4j让Twitter的登录。 <一href="https://github.com/Sheikh-Aman/Android_Samples/blob/master/1.%20Sign-inWithTwitterT4J/src/com/aman/samples/t4jsignin/Main.java"相对=nofollow>我用这个作为参考。

My aim is to allow Twitter logins using twitter4j. I used this as reference.

现在的问题是,在64行,它调用 getAuthenticationURl()。当我执行它带我到登录页面,而不是页面,在这里我可以允许/禁止我的应用程序访问PPL的账户。
不过,如果我把它改为 getAuthorizationUrl()它需要正确的页面。

The problem is that at line 64, it calls getAuthenticationURl(). When I execute it takes me to the login page, instead of the page where I can allow/disallow my app access to the ppl's account.
However, if I change it to getAuthorizationUrl() it takes to the correct page.

1),那么,什么是两者之间的区别是什么?

1) So what is the difference between the two?

2),这是很重要的问题。我提供低于code。

2) This is the important question. I am providing the code below.

package com.example.twitter;



import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class WriteTweet extends Activity{
    public final static String consumerKey = "xxxxxxxxxxxxxxxxxx";
    public final static String consumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    private final String CALLBACKURL = "T4J_OAuth://callback";
    Twitter twitter;
    RequestToken requestToken;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        loginUser();
    }

    private void loginUser() {
        // TODO Auto-generated method stub
        try {
            twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(consumerKey, consumerSecret);
            requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
            String authUrl = requestToken.getAuthorizationURL();
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                    .parse(authUrl)));
        } catch (TwitterException ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
            Log.e("in Main.OAuthLogin", ex.getMessage());
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        Log.e("hi","hi");
        super.onNewIntent(intent);
        Uri uri = intent.getData();
        try {
            String verifier = uri.getQueryParameter("oauth_verifier");
            AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,
                    verifier);
            String token = accessToken.getToken(), secret = accessToken
                    .getTokenSecret();
            displayTimeLine(token, secret); //after everything, display the first tweet 

        } catch (TwitterException ex) {
            Log.e("Main.onNewIntent", "" + ex.getMessage());
        }

    }
    @SuppressWarnings("deprecation")
    void displayTimeLine(String token, String secret) {
        Log.e("display","timeline");
    }

}

该清单是

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.twitter"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application

        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>


        <activity android:name=".ListTweets" >

        </activity>
        <activity android:name=".WriteTweet"> 
                <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="T4J_OAuth" android:host="callback" />
                </intent-filter>
        </activity>
        <activity android:name=".ViewFollowers">

        </activity>
    </application>

</manifest>

现在的问题是,它重定向我 https://mobile.twitter.com/ 我不能想出什么我做错了。这个问题存在,无论是否 getAuthenticationUrl() getAuthorizationUrl()被使用。我曾在在微博callback_url 字段中提供一个虚拟的URL。 如果我不提供 callbackurl 在code和Twitter页面,它会给我一个PIN码。但是,这不是我想要的。我需要我的Twitter重定向到我的应用程序。我怎样才能做到这一点?

The problem is that it redirects me to https://mobile.twitter.com/ and I cant figure out what am doing wrong. The problem exist no matter if whether getAuthenticationUrl() or getAuthorizationUrl() is used. I had provided a dummy url in the callback_url field in Twitter. If I didn't provide a callbackurl in code and in the twitter page it will give me a pin number. But that is not what I want. I need my twitter to redirect to my app. How can I achieve this?

推荐答案

这是我得到了解决此问题的办法,

This is the way I got around the problem,

  1. 创建一个Web视图,并从应用程序中加载叽叽喳喳的构象页。

  1. create a web-view and load the conformation page of twitter from within your app.

vw = (WebView)findViewById(R.id.loginView);
vw.setWebViewClient(new WebViewClient() {
    @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);
    }
}); vw.loadUrl(authUrl);

  • 在收到callback_url,得到'oauth_verifier

  • Once you receive a callback_url, get the ' oauth_verifier '

                Uri uri = Uri.parse(url);
                String verifier = uri.getQueryParameter("oauth_verifier");
    

  • 使用此验证来获得访问,并做你想做的。在我来说,我摧毁的WebView和负载我所需要的活动。

  • Use this verifier to get access and do what you want. In my case I destroy the webview and load the activity I require.

    这篇关于Twitter是不是重定向到回调URL的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆