2014-10-08 66 views
0

Iam在我的Android應用中實現了OAuth。當我登錄時,我得到一個帶有刷新令牌和失效時間的Access令牌。訪問令牌在1小時後過期。我知道如何從刷新令牌請求新的訪問令牌,但我不確定如何在到期時間內執行此操作。每一小時刷新訪問令牌OAuth Android

這裏是我的代碼: -

mMap = helper.getUserDetails(); 

        mAccessToken = mMap.get("accesstoken"); 
        mRefreshToken = mMap.get("refresh_token"); 
        mExpiresIn = mMap.get("expires_in"); 
        mExpiresOn = mMap.get("expires_on"); 
        mIdToken = mMap.get("id_token"); 
        mScope = mMap.get("scope"); 
        mTokenType = mMap.get("token_type"); 
        userName = mMap.get("username"); 
        firstName = mMap.get("name"); 





private class RefreshTokenTask extends AsyncTask<Void, Void, Void> 
    { 

     @Override 
     protected Void doInBackground(Void... params) 
     { 
      // TODO Auto-generated method stub 
      /** 
      * Check if access token is expired 
      * Request new access token by passing refresh token 
      */ 
      String mUrl = Constants.LOGIN_URL + Constants.TENANT +"/oauth2/token"; 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(mUrl); 
      int expiryHour = (Integer.parseInt(mExpiresIn)/60)/60; 



      try 
      { 
       List<NameValuePair> nvps = new ArrayList<NameValuePair>(4); 
       nvps.add(new BasicNameValuePair("client_id", Constants.CLIENT_ID)); 

       nvps.add(new BasicNameValuePair("grant_type", "authorization_code")); 
       nvps.add(new BasicNameValuePair("refresh_token", mRefreshToken)); 
       httppost.setEntity(new UrlEncodedFormEntity(nvps)); 

       // Execute HTTP Post Request 
       HttpResponse refreshResponse = httpclient.execute(httppost); 
       HttpEntity refreshEntity = refreshResponse.getEntity(); 
       result = EntityUtils.toString(refreshEntity); 

       //Deserialize the data into JSON 
       JSONObject refreshStatusObject = new JSONObject(result); 
       //Pull values out of the JSON 
       mAccessToken = refreshStatusObject.getString("access_token"); 
       Log.i(TAG, "Access Token: " + mAccessToken); 
       mExpiresIn = refreshStatusObject.getString("expires_in"); 
       mRefreshToken = refreshStatusObject.getString("refresh_token"); 
       mPortalId = refreshStatusObject.getString("portal_id"); 

       int firstIndex = mAccessToken.indexOf("."); 
       int secondIndex = mAccessToken.indexOf(".", firstIndex+2); 
       String claims = mAccessToken.substring(firstIndex + 1, secondIndex); 
       //Decode base64 URL ended claims 
       byte[] data = Base64.decode(claims, Base64.URL_SAFE); 

       String text = new String(data, "ASCII"); 
       //Display claims on screen 

       JSONObject jObject = new JSONObject(text); 
       //Get and display the logged in user name 
       userName = jObject.getString("unique_name"); 
       firstName = jObject.getString("given_name"); 

       helper.createLoginSession(mAccessToken, mExpiresIn, mExpiresOn, mIdToken, mRefreshToken, mResource, mScope, mTokenType, userName, firstName); 
      } 
      catch (UnsupportedEncodingException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 




      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) 
     { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      Intent i = new Intent(SplashScreen.this, SUpdate.class); 
      startActivity(i); 
      finish(); 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
     } 


    } 

回答

0

你應該使用的是Android Authenticator這一點。然後你將使用AccountManager來詢問令牌。您的自定義身份驗證器將會被調用。在該身份驗證程序中,您需要檢查您的當前訪問令牌是否已過期。如果是,請使用刷新令牌進行調用並獲取新令牌,然後更新帳戶管理器中的驗證令牌,然後將其返回給調用者。

因此,您的應用程序不需要知道如何以及何時更新令牌的詳細信息,它只是知道「我需要令牌,讓我從AccountManager獲取一個令牌」,然後客戶經理和驗證者接管爲你做剩下的事。

這是a good tutorial on building your own Authenticator