2012-10-22 45 views
1

這是我的代碼來獲取oauth標記並授權我的vimeo應用程序。這工作得很好:獲取vimeo api簽名

WebView webview = new WebView(this); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.setVisibility(View.VISIBLE); 
     setContentView(webview); 

     Log.i(TAG, "Retrieving request token from Vimeo servers"); 

     try { 

      final OAuthHmacSigner signer = new OAuthHmacSigner(); 
      signer.clientSharedSecret = Constants.CONSUMER_SECRET_VIMEO; 

      OAuthGetTemporaryToken temporaryToken = new OAuthGetTemporaryToken(Constants.REQUEST_URL_VIMEO); 
      temporaryToken.transport = new ApacheHttpTransport(); 
      temporaryToken.signer = signer; 
      temporaryToken.consumerKey = Constants.CONSUMER_KEY_VIMEO; 
      temporaryToken.callback = Constants.OAUTH_CALLBACK_URL; 
      OAuthCredentialsResponse tempCredentials = temporaryToken.execute(); 
      signer.tokenSharedSecret = tempCredentials.tokenSecret; 

      OAuthAuthorizeTemporaryTokenUrl authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(Constants.AUTHORIZE_URL_VIMEO); 
      authorizeUrl.temporaryToken = tempCredentials.token; 
      String authorizationUrl = authorizeUrl.build(); 
      Log.d("urlop", authorizationUrl); 

      /* WebViewClient must be set BEFORE calling loadUrl! */ 
      webview.setWebViewClient(new WebViewClient() { 

       @Override 
       public void onPageStarted(WebView view, String url,Bitmap bitmap) { 
        System.out.println("onPageStarted : " + url); 
       } 
       @Override 
       public void onPageFinished(WebView view, String url) 
       { 
        Log.d("url", url); 
        if (url.startsWith(Constants.OAUTH_CALLBACK_URL)) { 
         try { 

          if (url.indexOf("oauth_token=")!=-1) { 

           String requestToken = extractParamFromUrl(url,"oauth_token"); 
           String verifier= extractParamFromUrl(url,"oauth_verifier"); 

           signer.clientSharedSecret = Constants.CONSUMER_SECRET; 

           OAuthGetAccessToken accessToken = new OAuthGetAccessToken(Constants.ACCESS_URL); 
           accessToken.transport = new ApacheHttpTransport(); 
           Log.d("abc", ""); 
           accessToken.temporaryToken = requestToken; 
           Log.d("abc", accessToken.temporaryToken); 
           accessToken.signer = signer; 

           accessToken.consumerKey = Constants.CONSUMER_KEY; 
           accessToken.verifier = verifier; 
           Log.d("abc", accessToken.verifier); 

           OAuthCredentialsResponse credentials = accessToken.execute(); 

           signer.tokenSharedSecret = credentials.tokenSecret; 
           Log.d("abc", signer.tokenSharedSecret); 
           CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs); 
           credentialStore.write(new String[] {credentials.token,credentials.tokenSecret}); 
           view.setVisibility(View.INVISIBLE); 
           performApiCall(); 
           // startActivity(new Intent(OAuthAccessTokenActivityVimeo.this,Vimeo.class)); 
          } 
          else if (url.indexOf("error=")!=-1) 
          { 
           view.setVisibility(View.INVISIBLE); 
           new SharedPreferencesCredentialStore(prefs).clearCredentials(); 
           startActivity(new Intent(OAuthAccessTokenActivityVimeo.this,MainMenu.class)); 
          } 

         } catch (IOException e) { 
          e.printStackTrace(); 
         } 

        } 
        System.out.println("onPageFinished : " + url); 

       } 
       private String extractParamFromUrl(String url,String paramName) 
       { 
        String queryString = url.substring(url.indexOf("?", 0)+1,url.length()); 
        QueryStringParser queryStringParser = new QueryStringParser(queryString); 
        return queryStringParser.getQueryParamValue(paramName); 
       } 

      }); 

      webview.loadUrl(authorizationUrl); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

然而,在performApiCall()我需要這樣做:

String url = String.format("http://vimeo.com/api/rest/v2&format=json&full_response=1&method=vimeo.videos.search&oauth_consumer_key=%s&oauth_nonce=fb86e833df995307290763917343ae19&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1350908218&oauth_version=1.0&per_page=20&query=umar&sort=newest&summary_response=1", 
              Constants.CONSUMER_KEY 

              ); 

我怎樣才能得到oauth_nonceoauth_timestampoauth_signature

+0

的http://計算器。com/questions/7220295/vimeo-advanced-api-how-to-connect-via-oauth這對你有幫助嗎?或此http://developer.vimeo.com/apis/advanced#oauth – Slartibartfast

+0

沒有實際上我成功地用vimeo進行身份驗證......唯一的問題是如何傳遞此查詢,因爲我不知道我的oAuth會話的值 –

回答

0

http://developer.vimeo.com/apis/advanced

如果你人生地不熟的OAuth,我們建議您閱讀 hueniverse’s OAuth 1.0 Guide,然後再繼續。 OAuth是 複雜,並且他在解釋過程方面做得很好。 Twitter’s guide也很好。

同時還要注意以下資源(可以幫助你)

+0

隊友,我已經成功使用google oauth api驗證vimeo。但是我不知道如何獲得簽名時間戳和隨機數。 –

+0

@umar:你看過我的回答嗎? –

+0

@ s.d是的,我做了,內置谷歌API創建自己的簽名進行身份驗證。一旦認證,我不知道如何獲得這些值。那是我的問題是:) –

2

1)Re oauth_timestamp:AFAIK,你將你自己的時間戳設置爲當前時間戳。如果遇到問題(vimeo預計時間戳在它認爲是當前時間戳前後的時間差不超過幾秒鐘,請參閱the vimeo forum thread here),請嘗試擺弄您使用的系統時間(例如服務器時間)。

2)重新oauth_nonce:該現時值是一個「隨機字符串,由客戶端唯一生成以允許服務器以驗證請求以前從未被製造」(The OAuth 1.0 Protocol)。

3)Re oath_signature客戶端生成自己的簽名。從The OAuth 1.0 Protocol

客戶聲明哪個簽名方法經由 「oauth_signature_method」參數使用。然後它生成一個簽名(或 一個等效值的字符串),並將其包含在 「oauth_signature」參數中。服務器驗證爲每種方法指定的簽名爲 。

簡而言之:您不必從某處「獲取」這些值,您必須創建它們。

我真的很想再次向你推薦The OAuth 1.0 Protocol,它很容易閱讀,應該讓你對大多數查詢進行排序:)。

希望這會有所幫助。