2013-06-15 5 views
2

로그인 할 procedure은 무엇입니까 twitter api1.1을 던집니다. api 1이 없어서 twitter connection failed이 표시 될 오래된 API를 사용했습니다.android에서 twitter api를 사용하는 로그인 문제

private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() 
    { 
     public void onComplete(String value) 
     { 
      getTwitterDetail(); 
     } 
     public void onError(String value) {   
      Toast.makeText(LoginActivity.this, "Twitter connection failed", Toast.LENGTH_LONG).show(); 
     } 
    }; 

LOG

{"errors": [{"message": "The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.", "code": 68}]}

답변

1

당신이 오류 로그 API의 1 절에서 볼 수 있듯이 더 이상 활성 상태가없고 모두가 버전 1.1로 마이그레이션해야합니다. API v1.1. 연결하려면 OAUTH을 통해 로그인해야합니다. 따라서 dev.twitter.com에 앱을 등록해야합니다. 당신은 예를 here

public class Main extends Activity{ 
public static final String TAG = Main.class.getSimpleName(); 
public static final String TWITTER_OAUTH_REQUEST_TOKEN_ENDPOINT = "..."; //cannot share more then 2 lins, sorry 

public static final String TWITTER_OAUTH_ACCESS_TOKEN_ENDPOINT = "..."; 
public static final String TWITTER_OAUTH_AUTHORIZE_ENDPOINT = "..."; 
private CommonsHttpOAuthProvider commonsHttpOAuthProvider; 
private CommonsHttpOAuthConsumer commonsHttpOAuthConsumer; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    commonsHttpOAuthProvider = new CommonsHttpOAuthProvider(TWITTER_OAUTH_REQUEST_TOKEN_ENDPOINT, 
      TWITTER_OAUTH_ACCESS_TOKEN_ENDPOINT, TWITTER_OAUTH_AUTHORIZE_ENDPOINT); 
    commonsHttpOAuthConsumer = new CommonsHttpOAuthConsumer(getString(R.string.twitter_oauth_consumer_key), 
      getString(R.string.twitter_oauth_consumer_secret)); 
    commonsHttpOAuthProvider.setOAuth10a(true); 
    TwDialog dialog = new TwDialog(this, commonsHttpOAuthProvider, commonsHttpOAuthConsumer, 
      dialogListener, R.drawable.android); 
    dialog.show(); 

} 


private Twitter.DialogListener dialogListener = new Twitter.DialogListener() { 
    public void onComplete(Bundle values) { 
     String secretToken = values.getString("secret_token"); 
     Log.i(TAG,"secret_token=" + secretToken); 
     String accessToken = values.getString("access_token"); 
     Log.i(TAG,"access_token=" + accessToken); 
     new Tweeter(accessToken,secretToken).tweet(
       "Tweet from sample Android OAuth app. unique code: " + System.currentTimeMillis()); 
    } 

    public void onTwitterError(TwitterError e) { Log.e(TAG,"onTwitterError called for TwitterDialog", 
      new Exception(e)); } 

    public void onError(DialogError e) { Log.e(TAG,"onError called for TwitterDialog", new Exception(e)); } 

    public void onCancel() { Log.e(TAG,"onCancel"); } 
}; 

public static final Pattern ID_PATTERN = Pattern.compile(".*?\"id_str\":\"(\\d*)\".*"); 
public static final Pattern SCREEN_NAME_PATTERN = Pattern.compile(".*?\"screen_name\":\"([^\"]*).*"); 

public class Tweeter { 
    protected CommonsHttpOAuthConsumer oAuthConsumer; 

    public Tweeter(String accessToken, String secretToken) { 
     oAuthConsumer = new CommonsHttpOAuthConsumer(getString(R.string.twitter_oauth_consumer_key), 
       getString(R.string.twitter_oauth_consumer_secret)); 
     oAuthConsumer.setTokenWithSecret(accessToken, secretToken); 
    } 

    public boolean tweet(String message) { 
     if (message == null && message.length() > 140) { 
      throw new IllegalArgumentException("message cannot be null and must be less than 140 chars"); 
     } 
     // create a request that requires authentication 

     try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      Uri.Builder builder = new Uri.Builder(); 
      builder.appendPath("statuses").appendPath("update.json") 
        .appendQueryParameter("status", message); 
      Uri man = builder.build(); 
      HttpPost post = new HttpPost("http://twitter.com" + man.toString()); 
      oAuthConsumer.sign(post); 
      HttpResponse resp = httpClient.execute(post); 
      String jsonResponseStr = convertStreamToString(resp.getEntity().getContent()); 
      Log.i(TAG,"response: " + jsonResponseStr); 
      String id = getFirstMatch(ID_PATTERN,jsonResponseStr); 
      Log.i(TAG,"id: " + id); 
      String screenName = getFirstMatch(SCREEN_NAME_PATTERN,jsonResponseStr); 
      Log.i(TAG,"screen name: " + screenName); 

      final String url = MessageFormat.format("https://twitter.com/#!/{0}/status/{1}",screenName,id); 
      Log.i(TAG,"url: " + url); 

      Runnable runnable = new Runnable() { 
       public void run() { 
        ((TextView)Main.this.findViewById(R.id.textView)).setText("Tweeted: " + url); 
       } 
      }; 

      Main.this.runOnUiThread(runnable); 

      return resp.getStatusLine().getStatusCode() == 200; 

     } catch (Exception e) { 
      Log.e(TAG,"trying to tweet: " + message, e); 
      return false; 
     } 

    } 
} 

public static String convertStreamToString(java.io.InputStream is) { 
    try { 
     return new java.util.Scanner(is).useDelimiter("\\A").next(); 
    } catch (java.util.NoSuchElementException e) { 
     return ""; 
    } 
} 

public static String getFirstMatch(Pattern pattern, String str){ 
    Matcher matcher = pattern.matcher(str); 
    if(matcher.matches()){ 
     return matcher.group(1); 
    } 
    return null; 
} 
+0

안녕 @Defuera를 검색 할 수 있습니다! v1.1 API를 사용하여 로그인 할 수 있었지만 트위터에 이미지를 업로드 할 수 없었습니다. "https://api.twitter.com/1.1/statuses/update.json"을 사용하고 있습니다 –

+1

https :// /dev.twitter.com/docs/api/1/post/statuses/update_with_media - 이미지를 업로드 할 올바른 URL을 찾을 수 있습니다. – Defuera

+0

감사합니다. –

관련 문제