code: private static Date calculateExpiresAt( JSONObject jsonObj )
      {
          long expiresAt_s = jsonObj.optLong( OAuth2Credentials.EXPIRES_AT, -1 );
          // If nothing specified in json, check if expires_in is present:
          // this happens when token is received from oauth server
          if ( expiresAt_s < 0 && jsonObj.has( OAuth2Credentials.EXPIRES_IN ) ) {
              long expiresIn_s = jsonObj.getLong( OAuth2Credentials.EXPIRES_IN );
              // We take a margin to be safe: it appears that some providers do NOT take any margin
              // so token will not be early refreshed
              if ( expiresIn_s > 6 * 60 ) {  // should be always true
                  expiresIn_s -= 5 * 60;  // 5 minutes to be safe
              }
              expiresAt_s = System.currentTimeMillis() / 1000 + expiresIn_s;
          }
  
          if ( expiresAt_s < 0 ) {
              return null;
          }
          return new Date( expiresAt_s * 1000 );
      }
docs: Calculate expiration timestamp, if not defined.
  
  Several cases: absolute expiration time exists in json, or only relative, or no expiration.
  
  @param jsonObj
  @return expiration date, or null if none.