Search in sources :

Example 1 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project intermine by intermine.

the class Callback method getTokenResponse.

private OAuthAccessTokenResponse getTokenResponse(String redirect, OAuthAuthzResponse oar, OAuthProvider provider) throws OAuthSystemException, OAuthProblemException {
    OAuthClient oauthClient = new OAuthClient(new URLConnectionClient());
    OAuthClientRequest clientReq;
    TokenRequestBuilder requestBuilder = OAuthClientRequest.tokenLocation(provider.getTokenUrl()).setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(provider.getClientId()).setClientSecret(provider.getClientSecret()).setRedirectURI(redirect).setCode(oar.getCode());
    switch(provider.getMessageFormat()) {
        case BODY:
            clientReq = requestBuilder.buildBodyMessage();
            break;
        case QUERY:
            clientReq = requestBuilder.buildQueryMessage();
            break;
        default:
            throw new RuntimeException("Unknown message format");
    }
    LOG.info("Requesting access token: URI = " + clientReq.getLocationUri() + " BODY = " + clientReq.getBody());
    switch(provider.getResponseType()) {
        case FORM:
            return oauthClient.accessToken(clientReq, GitHubTokenResponse.class);
        case JSON:
            return oauthClient.accessToken(clientReq);
        default:
            throw new RuntimeException("Unknown response type");
    }
}
Also used : TokenRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 2 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project aos-MediaLib by nova-video-player.

the class TraktV2 method getAccessToken.

/**
 * Request an access token from trakt. Builds the request with {@link #getAccessTokenRequest(String, String, String,
 * String)} and executes it, then returns the response which includes the access token.
 *
 * <p> Supply the received access token to {@link #setAccessToken(String)}.
 *
 * <p> On failure re-authorization of your app is required (see {@link #getAuthorizationRequest(String, String,
 * String, String)}).
 *
 * @param clientId The OAuth client id obtained from trakt.
 * @param clientSecret The OAuth client secret obtained from trakt.
 * @param redirectUri The redirect URI previously used for obtaining the auth code.
 * @param authCode A valid authorization code (see {@link #getAuthorizationRequest(String, String, String,
 * String)}).
 */
public static OAuthAccessTokenResponse getAccessToken(String clientId, String clientSecret, String redirectUri, String authCode) throws OAuthSystemException, OAuthProblemException {
    OAuthClientRequest request = getAccessTokenRequest(clientId, clientSecret, redirectUri, authCode);
    OAuthClient client = new OAuthClient(new TraktHttpClient());
    return client.accessToken(request);
}
Also used : OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 3 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project java-demos by powerLeePlus.

the class AuthAccessController method getAccessToken.

/**
 * 三、授权许可(Authorization Grant)(对应父工程README.md的流程图)
 * 接受服务端返回的code,提交申请access token的请求
 * 3.服务端回调,传回code值
 * 4.根据code值,调用服务端服务,根据code获取access_token
 * 5.拿到access_token重定向到客户端的服务  /oauth-client/getUserInfo
 * 6.在该服务中 再调用服务端获取用户信息
 */
@RequestMapping("/callbackCode")
public Object getAccessToken(HttpServletRequest request) throws OAuthProblemException {
    String code = request.getParameter("code");
    System.out.println("==> 服务端回调,获取的code:" + code);
    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
    try {
        OAuthClientRequest accessTokenRequest = OAuthClientRequest.tokenLocation(server_accessTokenUrl).setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(client_clientId).setClientSecret(client_clientSecret).setCode(code).setRedirectURI(client_redirectUrl_getUserInfo).buildQueryMessage();
        System.out.println("==> 向服务端发起获取accessToken的请求:" + accessTokenRequest.getLocationUri());
        // 去服务端请求access token,并返回响应
        OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(accessTokenRequest, OAuth.HttpMethod.POST);
        // 获取服务端返回过来的access token
        String accessToken = oAuthResponse.getAccessToken();
        // 查看access token是否过期
        Long expiresIn = oAuthResponse.getExpiresIn();
        System.out.println("==> 客户端根据 code值 " + code + " 到服务端获取的access_token为:" + accessToken + " 过期时间为:" + expiresIn);
        System.out.println("==> 拿到access_token然后重定向到 客户端 [ " + client_redirectUrl_getUserInfo + " ]服务,传过去accessToken");
        // 客户端拿到token自动重定向到获取资源的URL。也可以交由server端自动重定向,取决于服务端如何实现的(是否会自动重定向)
        return "redirect:" + client_redirectUrl_getUserInfo + "?accessToken=" + accessToken;
    } catch (OAuthSystemException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project aos-Video by nova-video-player.

the class TraktSigninDialogPreference method onClick.

@Override
public void onClick() {
    try {
        OAuthClientRequest t = Trakt.getAuthorizationRequest(getSharedPreferences());
        final OAuthData oa = new OAuthData();
        OAuthCallback codeCallBack = new OAuthCallback() {

            @Override
            public void onFinished(final OAuthData data) {
                // TODO Auto-generated method stub
                if (data.code != null) {
                    final ProgressDialog mProgress = new ProgressDialog(getContext());
                    AsyncTask t = new AsyncTask() {

                        @Override
                        protected void onPreExecute() {
                            mProgress.show();
                        }

                        @Override
                        protected Object doInBackground(Object... params) {
                            OAuthAccessTokenResponse res = Trakt.getAccessToken(oa.code);
                            return res;
                        }

                        @Override
                        protected void onPostExecute(Object result) {
                            mProgress.dismiss();
                            if (result != null && result instanceof OAuthAccessTokenResponse) {
                                OAuthAccessTokenResponse res = (OAuthAccessTokenResponse) result;
                                if (res.getAccessToken() != null) {
                                    Trakt.setAccessToken(getSharedPreferences(), res.getAccessToken());
                                    Trakt.setRefreshToken(getSharedPreferences(), res.getRefreshToken());
                                    TraktSigninDialogPreference.this.notifyChanged();
                                }
                            }
                        }
                    };
                    t.execute();
                } else {
                    new AlertDialog.Builder(getContext()).setNegativeButton(android.R.string.ok, null).setMessage(R.string.dialog_subloader_nonetwork_title).setIcon(android.R.drawable.ic_dialog_alert).show();
                }
            }
        };
        od = new OAuthDialog(getContext(), codeCallBack, oa, t);
        od.show();
        od.setOnDismissListener(mOnDismissListener);
        od.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialogInterface) {
                mOnDismissListener.onDismiss(dialogInterface);
            }
        });
    } catch (OAuthSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : DialogInterface(android.content.DialogInterface) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) AsyncTask(android.os.AsyncTask) ProgressDialog(android.app.ProgressDialog) OAuthAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 5 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project javlo by Javlo.

the class AbstractSocialNetwork method getAccessToken.

public String getAccessToken(String code, OAuthClient oAuthClient) throws OAuthSystemException, OAuthProblemException {
    String clientId = getClientId();
    if (clientId == null || clientId.isEmpty()) {
        return null;
    }
    String clientSecret = getClientSecret();
    if (clientSecret == null || clientSecret.isEmpty()) {
        return null;
    }
    TokenRequestBuilder builder = createTokenRequest();
    configureTokenRequest(builder, clientId, clientSecret, code);
    OAuthClientRequest request = buildTokenRequest(builder);
    OAuthAccessTokenResponse response = executeTokenRequest(oAuthClient, request);
    String accessToken = response.getAccessToken();
    Long expiresIn = response.getExpiresIn();
    // TODO remove sysouts
    System.out.println("accessToken = " + accessToken);
    System.out.println("expiresIn = " + expiresIn);
    return accessToken;
}
Also used : TokenRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder) OAuthAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Aggregations

OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)8 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)6 OAuthAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse)6 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)5 TokenRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder)3 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)3 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)2 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 AsyncTask (android.os.AsyncTask)1 Base64 (org.apache.commons.codec.binary.Base64)1 GitHubTokenResponse (org.apache.oltu.oauth2.client.response.GitHubTokenResponse)1 BadRequestException (org.intermine.webservice.server.exceptions.BadRequestException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1