UserCredentials fetchUserCredentials ( String code ) { HttpPost post = new HttpPost ( accessTokenUrl ) ; List < NameValuePair > parameters = new ArrayList < NameValuePair > ( ) ; parameters . add ( new BasicNameValuePair ( OAuth2 . CLIENT_ID , appInfo . getAppId ( ) ) ) ; parameters . add ( new BasicNameValuePair ( OAuth2 . CLIENT_SECRET , appInfo . getAppSecret ( ) ) ) ; parameters . add ( new BasicNameValuePair ( OAuth2 . CODE , code ) ) ; parameters . add ( new BasicNameValuePair ( OAuth2 . GRANT_TYPE , OAuth2 . AUTHORIZATION_CODE ) ) ; if ( appInfo . getRedirectUrl ( ) != null ) { parameters . add ( new BasicNameValuePair ( OAuth2 . REDIRECT_URI , appInfo . getRedirectUrl ( ) ) ) ; } try { post . setEntity ( new UrlEncodedFormEntity ( parameters , PcsUtils . UTF8 . name ( ) ) ) ; } catch ( UnsupportedEncodingException ex ) { throw new CStorageException ( "Can't encode parameters" , ex ) ; } HttpResponse response ; try { response = httpClient . execute ( post ) ; } catch ( IOException e ) { throw new CStorageException ( "HTTP request while fetching token has failed" , e ) ; } // FIXME check status code here final String json ; try { json = EntityUtils . toString ( response . getEntity ( ) , PcsUtils . UTF8 . name ( ) ) ; } catch ( IOException e ) { throw new CStorageException ( "Can't retrieve json string in HTTP response entity" , e ) ; } LOGGER . debug ( "fetchUserCredentials - json: {}" , json ) ; Credentials credentials = Credentials . createFromJson ( json ) ; userCredentials = new UserCredentials ( appInfo , null , // userId is unknown yet credentials ) ; return userCredentials ; }
Fetches user credentials