2013-01-17 1 views
0

나는 트위터를 사용하여 안드로이드 애플 리케이션에서 데이터를 공유하려고합니다. 그래서 "트위터 앱 등록 페이지"에 제공된 URL 리디렉션과 같은 기본 정보는 무엇입니까? 모든 것이 정상적으로 작동하지만 트위터의 LOGIN PAGE에서 사용자 이름과 암호를 제공 한 후 다음 페이지로 리다이렉트하지 않고 대신 "이 페이지에 너무 많은 서버 리디렉션이 포함되었습니다"라는 오류 메시지가 표시됩니다.트위터 : "이 페이지는 너무 많은 서버 리디렉션을 포함합니다"오류

리디렉션 URL은 "https://www.example.com/"입니다.

제안 사항?

public class constants { 
public static final String CONSUMER_KEY = "key"; 
public static final String CONSUMER_SECRET= "secret"; 

public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token"; 
public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token"; 
public static final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize"; 


public static final String OAUTH_CALLBACK_URL = "x-latify-oauth-twitter"; 
private static final String CALLBACK_SCHEME = null; 
public static final Object OAUTH_CALLBACK_SCHEME = CALLBACK_SCHEME + "://callback"; 

}

MainActivity

import java.util.Date; 

import oauth.signpost.OAuth; 
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; 
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; 
import android.os.Bundle; 
import android.os.Handler; 
import android.preference.PreferenceManager; 
    import android.provider.SyncStateContract.Constants; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
    import android.widget.Toast; 

    public class MainActivity extends Activity { 


private SharedPreferences prefs; 
    private final Handler mTwitterHandler = new Handler(); 
    private TextView loginStatus; 

    final Runnable mUpdateTwitterNotification = new Runnable() { 
    public void run() { 
     Toast.makeText(getBaseContext(), "Tweet sent !", Toast.LENGTH_LONG).show(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    this.prefs = PreferenceManager.getDefaultSharedPreferences(this); 

    loginStatus = (TextView)findViewById(R.id.ls); 
    Button tweet = (Button) findViewById(R.id.tweet); 
    Button clearCredentials = (Button) findViewById(R.id.cc); 

     tweet.setOnClickListener(new View.OnClickListener() { 

    * to the twitter login page. Once the user authenticated, he'll authorize the  Android  application to send 
    * tweets on the users behalf. 
    */ 
     public void onClick(View v) { 
     if (TwitterUtils.isAuthenticated(prefs)) { 
     sendTweet(); 
     } else { 
    Intent i = new Intent(getApplicationContext(), PrepareRequestTokenActivity.class); 
    i.putExtra("tweet_msg",getTweetMsg()); 
    startActivity(i); 
     } 
     } 
    }); 

    clearCredentials.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     clearCredentials(); 
     updateLoginStatus(); 
     } 
    }); 
    } 

    @Override 
    protected void onResume() { 
super.onResume(); 
updateLoginStatus(); 
    } 

public void updateLoginStatus() { 
    loginStatus.setText("Logged into Twitter : " + TwitterUtils.isAuthenticated(prefs)); 
    } 


private String getTweetMsg() { 
return "Tweeting from Android App at " + new Date().toLocaleString(); 
    } 

public void sendTweet() { 
Thread t = new Thread() { 
    public void run() { 

    try { 
    TwitterUtils.sendTweet(prefs,getTweetMsg()); 
    mTwitterHandler.post(mUpdateTwitterNotification); 
    } catch (Exception ex) { 
    ex.printStackTrace(); 
    } 
    } 

}; 
t.start(); 
} 

private void clearCredentials() { 
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
final Editor edit = prefs.edit(); 
edit.remove(OAuth.OAUTH_TOKEN); 
edit.remove(OAuth.OAUTH_TOKEN_SECRET); 
edit.commit(); 
} 
} 



import oauth.signpost.OAuthConsumer; 
import oauth.signpost.OAuthProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.util.Log; 


public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void> { 

    final String TAG = getClass().getName(); 
    private Context context; 
    private OAuthProvider provider; 
    private OAuthConsumer consumer; 

    /** 
    * 
    * We pass the OAuth consumer and provider. 
    * 
    * @param context 
    * Required to be able to start the intent to launch the browser. 
    * @param provider 
    * The OAuthProvider object 
    * @param consumer 
    * The OAuthConsumer object 
    */ 
    public OAuthRequestTokenTask(Context context,OAuthConsumer consumer,OAuthProvider provider) { 
    this.context = context; 
    this.consumer = consumer; 
    this.provider = provider; 
    } 

    /** 
    * 
    * Retrieve the OAuth Request Token and present a browser to the user to authorize the token. 
    * 
    */ 
    @Override 
    protected Void doInBackground(Void... params) { 

    try { 
    Log.i(TAG, "Retrieving request token from Google servers"); 
    final String url = provider.retrieveRequestToken(consumer, constants.OAUTH_CALLBACK_URL); 
    Log.i(TAG, "Popping a browser with the authorize URL : " + url); 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND); 
    context.startActivity(intent); 
    } catch (Exception e) { 
    Log.e(TAG, "Error during OAUth retrieve request token", e); 
    } 

    return null; 
    } 

    } 




import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.provider.SyncStateContract.Constants; 
import android.util.Log; 
import oauth.signpost.OAuth; 
import oauth.signpost.OAuthConsumer; 
import oauth.signpost.OAuthProvider; 
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; 
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; 

public class PrepareRequestTokenActivity extends Activity { 
final String TAG = getClass().getName(); 

private OAuthConsumer consumer; 
private OAuthProvider provider; 

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
try { 
this.consumer = new CommonsHttpOAuthConsumer(constants.CONSUMER_KEY, constants.CONSUMER_SECRET); 
this.provider = new   CommonsHttpOAuthProvider(constants.REQUEST_URL,constants.ACCESS_URL,constants.AUTHORIZE_URL  ); 
} catch (Exception e) { 
Log.e(TAG, "Error creating consumer/provider",e); 
} 

    Log.i(TAG, "Starting task to retrieve request token."); 
    new OAuthRequestTokenTask(this,consumer,provider).execute(); 
    } 

    /** 
    * Called when the OAuthRequestTokenTask finishes (user has authorized the request token). 
* The callback URL will be intercepted here. 
    */ 
@Override 
    public void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    final Uri uri = intent.getData(); 
    if (uri != null && uri.getScheme().equals(constants.OAUTH_CALLBACK_SCHEME)) { 
    Log.i(TAG, "Callback received : " + uri); 
    Log.i(TAG, "Retrieving Access Token"); 
    new RetrieveAccessTokenTask(this,consumer,provider,prefs).execute(uri); 
    finish(); 
    } 
     } 

      public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> { 

     private Context context; 
     private OAuthProvider provider; 
     private OAuthConsumer consumer; 
     private SharedPreferences prefs; 

    public RetrieveAccessTokenTask(Context context, OAuthConsumer     consumer,OAuthProvider provider, SharedPreferences prefs) { 
    this.context = context; 
    this.consumer = consumer; 
    this.provider = provider; 
    this.prefs=prefs; 
     } 


     /** 
     * Retrieve the oauth_verifier, and store the oauth and oauth_token_secret 
    * for future API calls. 
     */ 
     protected Void doInBackground(Uri...params) { 
     final Uri uri = params[0]; 
     final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER); 

      try { 
     provider.retrieveAccessToken(consumer, oauth_verifier); 

     final Editor edit = prefs.edit(); 
     edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken()); 
     edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret()); 
    edit.commit(); 

    String token = prefs.getString(OAuth.OAUTH_TOKEN, ""); 
    String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); 

    consumer.setTokenWithSecret(token, secret); 
    context.startActivity(new Intent(context,MainActivity.class)); 

    executeAfterAccessTokenRetrieval(); 

    Log.i(TAG, "OAuth - Access Token Retrieved"); 

    } catch (Exception e) { 
    Log.e(TAG, "OAuth - Access Token Retrieval Error", e); 
    } 

    return null; 
     } 


    private void executeAfterAccessTokenRetrieval() { 
    String msg = getIntent().getExtras().getString("tweet_msg"); 
    try { 
     TwitterUtils.sendTweet(prefs, msg); 
     } catch (Exception e) { 
    Log.e(TAG, "OAuth - Error sending to Twitter", e); 
    } 
    } 
    } 

twitterUtils.java

import oauth.signpost.OAuth; 
import twitter4j.Twitter; 
import twitter4j.TwitterException; 
import twitter4j.TwitterFactory; 
import twitter4j.http.AccessToken; 
import android.content.SharedPreferences; 
import android.provider.SyncStateContract.Constants; 

    public class TwitterUtils { 
public static boolean isAuthenticated(SharedPreferences prefs) { 

String token = prefs.getString(OAuth.OAUTH_TOKEN, ""); 
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); 

AccessToken a = new AccessToken(token,secret); 
Twitter twitter = new TwitterFactory().getInstance(); 
twitter.setOAuthConsumer(constants.CONSUMER_KEY, constants.CONSUMER_SECRET); 
twitter.setOAuthAccessToken(a); 

try { 
twitter.getAccountSettings(); 
return true; 
} catch (TwitterException e) { 
return false; 
} 
} 

public static void sendTweet(SharedPreferences prefs,String msg) throws Exception { 
String token = prefs.getString(OAuth.OAUTH_TOKEN, ""); 
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); 

AccessToken a = new AccessToken(token,secret); 
Twitter twitter = new TwitterFactory().getInstance(); 
twitter.setOAuthConsumer(constants.CONSUMER_KEY, constants.CONSUMER_SECRET); 
    twitter.setOAuthAccessToken(a); 
    twitter.updateStatus(msg); 
} 
} 
+0

포스트 일부 코드는 그래서 우리는 더 당신을 도울 수 있습니다. –

+0

constants.java 코드 추가 @ DipakKeshariya –

답변

0

변경 귀하의 콜백 URL 아래에 쓰기 당신의 URL 대신 URL을 다시 호출합니다.

public static final String CALLBACK_URL = "x-oauthflow-twitter://callback"; 

자세한 내용은 아래 링크를 참조하십시오.

Twitter Integration in Android

+0

로그인 후 공백 페이지 만 표시 @ Dipak keshariya –

+0

@DDA 위의 Twitter 통합 링크 예제를 참조하십시오. –

+0

이들은 내 전체 코드입니다 @ Dipak Keshariya –

관련 문제