Java Code Examples for net.oauth.OAuthAccessor#newRequestMessage()

The following examples show how to use net.oauth.OAuthAccessor#newRequestMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OAuthClientUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static String doGetAuthorizationHeader(OAuthAccessor accessor,
        String method, String requestURI, Map<String, String> parameters) {
    try {
        OAuthMessage msg = accessor.newRequestMessage(method, requestURI, parameters.entrySet());
        StringBuilder sb = new StringBuilder();
        sb.append(msg.getAuthorizationHeader(null));
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            if (!entry.getKey().startsWith("oauth_")) {
                sb.append(", ");
                sb.append(OAuth.percentEncode(entry.getKey())).append("=\"");
                sb.append(OAuth.percentEncode(entry.getValue())).append('"');
            }
        }
        return sb.toString();
    } catch (Exception ex) {
        throw new ProcessingException(ex);
    }
}
 
Example 2
Source File: GoogleOAuthSubmitter.java    From jivejdon with Apache License 2.0 5 votes vote down vote up
public OAuthUserVO getUserInfo(OAuthAccessor accessor) throws Exception {
	OAuthUserVO weiboUser = null;
	InputStream in = null;
	try {
		accessor.consumer.setProperty(OAUTH_SIGNATURE_METHOD, HMAC_SHA1);
		OAuthMessage message = accessor.newRequestMessage(OAuthMessage.GET, goolgeOAuthParamVO.userInfo, null);
		OAuthMessage result = CLIENT.invoke(message, ParameterStyle.AUTHORIZATION_HEADER);

		in = result.getBodyAsStream();
		String jsonStr = getString(new InputStreamReader(in, Charset.forName("UTF-8")));
		JSONObject json = new JSONObject(jsonStr);
		String id = json.getString("id");
		String atName = json.getString("name");
		String description = json.getString("name");
		String url = "";
		if (!UtilValidate.isEmpty(json.getString("link")))
			url = json.getString("link");// t.qq.com/name
		String profileurl = "";
		if (!UtilValidate.isEmpty(json.getString("picture")))
			profileurl = json.getString("picture");
		weiboUser = new OAuthUserVO(id, atName, description, url, profileurl);

	} catch (Exception e) {
		throw new Exception(e);
	} finally {
		if (in != null)
			in.close();
	}
	return weiboUser;

}
 
Example 3
Source File: OAuthClient.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
    * Construct a request message, send it to the service provider and get the
    * response.
    * 
    * @param httpMethod
    *            the HTTP request method, or null to use the default method
    * @return the response
    * @throws URISyntaxException
    *             the given url isn't valid syntactically
    * @throws OAuthProblemException
    *             the HTTP response status code was not 200 (OK)
    */
   @SuppressWarnings("rawtypes")
public OAuthMessage invoke(OAuthAccessor accessor, String httpMethod,
           String url, Collection<? extends Map.Entry> parameters)
   throws IOException, OAuthException, URISyntaxException {
       OAuthMessage request = accessor.newRequestMessage(httpMethod, url, parameters);
       Object accepted = accessor.consumer.getProperty(OAuthConsumer.ACCEPT_ENCODING);
       if (accepted != null) {
           request.getHeaders().add(new OAuth.Parameter(HttpMessage.ACCEPT_ENCODING, accepted.toString()));
       }
       Object ps = accessor.consumer.getProperty(PARAMETER_STYLE);
       net.oauth.ParameterStyle style = (ps == null) ? net.oauth.ParameterStyle.BODY
               : Enum.valueOf(net.oauth.ParameterStyle.class, ps.toString());
       return invoke(request, style);
   }
 
Example 4
Source File: OAuthClient.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
    * Construct a request message, send it to the service provider and get the
    * response.
    * 
    * @param httpMethod
    *            the HTTP request method, or null to use the default method
    * @return the response
    * @throws URISyntaxException
    *             the given url isn't valid syntactically
    * @throws OAuthProblemException
    *             the HTTP response status code was not 200 (OK)
    */
   @SuppressWarnings("rawtypes")
public OAuthMessage invoke(OAuthAccessor accessor, String httpMethod,
           String url, Collection<? extends Map.Entry> parameters)
   throws IOException, OAuthException, URISyntaxException {
       OAuthMessage request = accessor.newRequestMessage(httpMethod, url, parameters);
       Object accepted = accessor.consumer.getProperty(OAuthConsumer.ACCEPT_ENCODING);
       if (accepted != null) {
           request.getHeaders().add(new OAuth.Parameter(HttpMessage.ACCEPT_ENCODING, accepted.toString()));
       }
       Object ps = accessor.consumer.getProperty(PARAMETER_STYLE);
       net.oauth.ParameterStyle style = (ps == null) ? net.oauth.ParameterStyle.BODY
               : Enum.valueOf(net.oauth.ParameterStyle.class, ps.toString());
       return invoke(request, style);
   }
 
Example 5
Source File: GetProtectedResourceController.java    From cxf with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/getProtectedResource")
protected ModelAndView handleRequest(@ModelAttribute("oAuthParams") OAuthParams oAuthParams,
                                     HttpServletRequest request)
    throws Exception {

    OAuthServiceProvider provider = new OAuthServiceProvider(
        oAuthParams.getTemporaryCredentialsEndpoint(),
        oAuthParams.getResourceOwnerAuthorizationEndpoint(), null);

    OAuthConsumer consumer = new OAuthConsumer(null, oAuthParams.getClientID(),
        oAuthParams.getClientSecret(),
        provider);
    OAuthAccessor accessor = new OAuthAccessor(consumer);
    accessor.requestToken = oAuthParams.getOauthToken();
    accessor.tokenSecret = oAuthParams.getOauthTokenSecret();

    Map<String, String> parameters = new HashMap<>();
    parameters.put(OAuth.OAUTH_SIGNATURE_METHOD, oAuthParams.getSignatureMethod());
    parameters.put(OAuth.OAUTH_NONCE, UUID.randomUUID().toString());
    parameters.put(OAuth.OAUTH_TIMESTAMP, String.valueOf(System.currentTimeMillis() / 1000));
    parameters.put(OAuth.OAUTH_TOKEN, oAuthParams.getOauthToken());
    parameters.put(OAuth.OAUTH_CONSUMER_KEY, oAuthParams.getClientID());

    OAuthMessage msg = null;
    String method = request.getParameter("op");


    if ("GET".equals(method)) {
        msg = accessor
            .newRequestMessage(OAuthMessage.GET, oAuthParams.getGetResourceURL(), parameters.entrySet());
    } else {
        msg = accessor
            .newRequestMessage(OAuthMessage.POST, oAuthParams.getPostResourceURL(),
                parameters.entrySet());
    }


    OAuthClient client = new OAuthClient(new URLConnectionClient());

    msg = client.access(msg, ParameterStyle.QUERY_STRING);

    StringBuilder bodyBuffer = readBody(msg);

    oAuthParams.setResourceResponse(bodyBuffer.toString());
    String authHeader = msg.getHeader("WWW-Authenticate");
    String oauthHeader = msg.getHeader("OAuth");
    String header = "";

    if (authHeader != null) {
        header += "WWW-Authenticate:" + authHeader;
    }

    if (oauthHeader != null) {
        header += "OAuth:" + oauthHeader;
    }

    oAuthParams.setHeader(header);
    oAuthParams.setResponseCode(((OAuthResponseMessage)msg).getHttpResponse().getStatusCode());

    return new ModelAndView("accessToken");
}