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();
}
}