2010-10-24 70 views
1

嗨 在我的android應用程序中,我可以在登錄後立即撥打Twitter api。我正在使用OAuthConsumer的相同實例。但是當我爲隨後的調用創建OAuthconsumer並使用setTokenWithSecret時,我得到錯誤的簽名錯誤。在Android上訪問Twitter api時簽名不正確

我花了幾個小時試圖調試,但沒有運氣......任何幫助表示讚賞。

以下是代碼...在onnewIntent if語句1 == 1級的作品,但我得到不正確的簽名錯誤。如果我打電話覈實Twitter的適配器方法

private static String TAG = "OAuthForTwitter"; 

private CommonsHttpOAuthConsumer httpOauthConsumer; 
private OAuthProvider httpOauthprovider; 
public final static String consumerKey = ""; 
public final static String consumerSecret = ""; 
private final String CALLBACKURL = "myapp://mainactivity"; 


@Override 
protected void onNewIntent(Intent intent) { 

    super.onNewIntent(intent); 
    Log.d(TAG, "onNewIntent"); 

    Uri uri = intent.getData(); 

    if (uri != null && uri.toString().startsWith(CALLBACKURL)) { 

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

     Log.d(TAG, "onNewIntent " + " verifier " + verifier); 
     try { 

      httpOauthprovider.retrieveAccessToken(httpOauthConsumer, 
        verifier); 
      String userKey = httpOauthConsumer.getToken(); 
      String userSecret = httpOauthConsumer.getConsumerSecret(); 

      if (1 == 1) { 
       String surl = "http://api.twitter.com/1/account/verify_credentials.xml"; 

       HttpGet request = null; 
       HttpClient httpClient = null; 
       HttpResponse response = null; 
       request = new HttpGet(surl); 
       httpOauthConsumer.sign(request); 
       System.out.println("Sending request to Twitter..."); 
       httpClient = new DefaultHttpClient(); 

       response = httpClient.execute(request); 
       String sresponse = parseResponseToString(response); 
       Log.d(TAG, sresponse); 
      } else { 

       TwitterAdapter adapter = new TwitterAdapter(null, 
         consumerKey, consumerSecret, userKey, userSecret); 
       String s = adapter.VerifyUser(); 
       Log.d(TAG, s); 
      } 

     } catch (Exception e) { 
      Log.d(TAG, "onNewIntent error " + e.getMessage()); 
     } 

    } 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    doOauth(); 
} 

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))); 
     Log.d(TAG, "sent doOauth"); 
    } catch (Exception e) { 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
    Log.d(TAG, " doOauth Complete"); 
} 


public class TwitterAdapter { 

    oauth.signpost.commonshttp.CommonsHttpOAuthConsumer httpOauthConsumer; 

    public TwitterAdapter(String username, String consumerkey,String consumersecret, String accesstoken, String accesssecret) { 

     httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerkey, consumersecret); 

     httpOauthConsumer.setTokenWithSecret(accesstoken, consumersecret); 
    } 


    public String VerifyUser() throws ClientProtocolException, IOException, 
      OAuthMessageSignerException, OAuthExpectationFailedException, 
      OAuthCommunicationException { 

     String surl = "http://api.twitter.com/1/account/verify_credentials.xml"; 

     HttpGet request = null; 
     HttpClient httpClient = null; 
     HttpResponse response = null; 
     request = new HttpGet(surl); 
     httpOauthConsumer.sign(request); 
     System.out.println("Sending request to Twitter..."); 
     httpClient = new DefaultHttpClient(); 

     response = httpClient.execute(request); 
     return parseResponseToString(response); 

    } 
} 

}

回答

0

您是否嘗試過使用Scribe

它有一個working example使用Twitter和它的android-ready。

+0

我想我會試試看。我一直無法弄清楚爲什麼上面的代碼不工作。 – Yogesh 2010-10-27 15:01:20

+0

因爲我是抄寫員的創造者,所以我可能會幫助你。 :) – 2010-10-27 15:08:03

+0

在您的例子u必須下面的代碼OAuthService服務=新ServiceBuilder() .provider(TwitterApi.class) .apiKey( 「6icbcAXyZx67r8uTAUM5Qw」) .apiSecret( 「SCCAdUUc6LXxiazxH3N0QfpNUvlUy84mZ2XZKiv39s」) .build(); – Yogesh 2010-10-29 04:03:55

0

我試過你的代碼......它的工作原理是,如果我使用它,但如果我改變它來適合我的應用程序它不起作用。我不迴應verify_credentials調用的響應。我一定錯過了關於OAuth的東西。但這裏是我在做什麼

public class scribeauth extends Activity { 
    private static String TAG = "OAuthForTwitter"; 
    public final static String consumerKey = ""; 
    public final static String consumerSecret = ""; 
    private final String CALLBACKURL = "myapp://mainactivity"; 
    private static final String AUTHORIZE_URL = "https://twitter.com/oauth/authorize?oauth_token="; 
    private static final String PROTECTED_RESOURCE_URL = "http://api.twitter.com/1/account/verify_credentials.xml"; 
    OAuthService service = null; 
    Token requestToken = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     doOauth(); 
    } 

    private void doOauth() { 
     try { 

      service = new ServiceBuilder().provider(TwitterApi.class) 
        .apiKey(consumerKey) 
        .apiSecret(consumerSecret) 
        .callback(CALLBACKURL).build(); 

      requestToken = service.getRequestToken(); 

      this.startActivity(new Intent(Intent.ACTION_VIEW, Uri 
        .parse(AUTHORIZE_URL + requestToken.getToken()))); 

     } catch (Exception e) { 
      Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
     Log.d(TAG, " doOauth Complete"); 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 

     super.onNewIntent(intent); 
     Log.d(TAG, "onNewIntent"); 

     Uri uri = intent.getData(); 

     if (uri != null && uri.toString().startsWith(CALLBACKURL)) { 

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

      Verifier verifier = new Verifier(sverifier); 

      Token accessToken = service.getAccessToken(requestToken, verifier); 

      String secret = accessToken.getSecret(); 
      String token = accessToken.getToken(); 

      new TwitterAdapter().VerifyUser(consumerKey, consumerSecret, token, secret); 

     } 
    } 

    public class TwitterAdapter { 
     public String VerifyUser(String consumerkey, String consumersecret, 
       String accesstoken, String accesssecret) { 

      service = new ServiceBuilder().provider(TwitterApi.class) 
      .apiKey(consumerkey) 
      .apiSecret(consumersecret) 
      .callback(CALLBACKURL).build(); 
      OAuthRequest request = new OAuthRequest(Verb.GET,PROTECTED_RESOURCE_URL); 

      Token accessToken = new Token(accesstoken, accesssecret); 
      service.signRequest(accessToken, request); 
      Response response = request.send(); 
      String sresponse = response.getBody(); 
      Log.d(TAG, sresponse); 
      return sresponse; 
     } 
    } 
} 
+0

你得到的錯誤是什麼? – 2010-10-29 14:33:47

+0

哦,你不需要每次創建服務。只需在構造函數中執行一次(它是線程安全的,所以不用擔心) – 2010-10-29 14:34:31

+0

我得到401錯誤和response.getBody()返回null – Yogesh 2010-10-29 21:09:22

0

只是試圖linkedinexample.java,不能行傳37:驗證驗證=新的驗證(in.nextLine());

應用程序停止。我是唯一一個??

+0

我的代碼中有一個新的TwitterAdapter()。VerifyUser(consumerKey,consumerSecret,secret,token); 它應該是新的TwitterAdapter()。VerifyUser(consumerKey,consumerSecret,token,secret); – Yogesh 2010-11-18 16:20:28