I am trying to get the access_token from facebook. First I redirect to facebook using an url as the following
https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=7316713919&redirect_uri=http://whomakescoffee.com:8080/app/welcome.jsf&scope=publish_stream
Then I have a listener that gets the url.
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request =
(HttpServletRequest) fc.getExternalContext().getRequest();
String url = request.getRequestURL().toString();
if (url.contains("access_token")) {
int indexOfEqualsSign = url.indexOf("=");
int indexOfAndSign = url.indexOf("&");
accessToken = url.substring(indexOfEqualsSign + 1, indexOfAndSign);
handleFacebookLogin(accessToken, fc);
}
But it never gets inside the if..
How do I retrieve the parameter when it comes after a # instead of a usual parameter after ?.
The url looks something like
http://benbiddington.wordpress.com/#access_token=
116122545078207|
2.1vGZASUSFMHeMVgQ_9P60Q__.3600.1272535200-500880518|
QXlU1XfJR1mMagHLPtaMjJzFZp4
The URL is incorrectly encoded. It's XML-escaped instead of URL-encoded. The # is a reserved character in URL's which represents the client-side fragment which is never sent back to the server side.
The URL should more look like this:
https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=7316713919&redirect_uri=http%3a%2f%2fwhomakescoffee.com%3a8080%2fapp%2fwelcome.jsf%26scope%3dpublish_stream
You can use java.net.URLEncoder for this.
Related
I try make request using client library javax.ws.rs
I'm trying to add a parameter name containing characters [ and ] to query parameters, for encdoded special characters i am used URLEncoder.encode(). But after the request, the response contains data without this parameter, the server ignored this request parameter. I made a request on the command line using "curl" on purpose with an error in this parameter and got the same result as from the request in the application. The error is clearly in the encoded parameter name, but I don’t understand how to correctly add parameters containing special characters.
The Code:
WebTarget webTarget = new Client().target(uri);
String key = "filters[Time].Start";
key = URLEncoder.encode(key,StandardCharsets.UTF_8.toString());
String value = "2022-08-27+17:00:00";
value = URLEncoder.encode(value,StandardCharsets.UTF_8.toString());
System.out.println(key + " " + value);
webTarget = webTarget.queryParam("per_page","10");
webTarget = webTarget.queryParam(key,value);
webTarget = webTarget.queryParam("order_by","time");
invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();
Result sout : filters%5BTime%5D.Start 2022-08-27%2B17%3A00%3A00
By advise #cyberbrain, i did checked my request in server by help 'tcpdump' utilite with key '-A'.
I did request by curl and after by my java application and compare data.
In my case problems was in value parametrs, my value equels "2022-08-27+17:00:00". After add in queryparametr this symbol '+' encoded to code '%2B' and this don't like my server. I just replaced symbol '+' to symbol space (code %20) in value ("2022-08-27 17:00:00"). After that i geted correct data from my server
We are getting url from JSON Response and which we open in in Chrome.The page loads , there is submit button which we click then it redirect to url as :-
https://www.google.com/AB1234
We need the need to retrieve only "AB1234" value from url.
tried following code to get value ="AB1234"
String url = driver.getCurrentUrl();
int index=url.lastIndexOf("/");
String result = url.substring(0,index);
but here getting initial part of url:https://www.google.com/
You need to call substring function with index +1 .
Try below code :
String url = driver.getCurrentUrl();
int index = url.lastIndexOf("/");
String result = url.substring(index + 1);
To parse a URI, it's likely a good idea to use a URI parser.
Given http://example.com/bar
String path = URI.create(driver.getCurrentUrl()).getPath();
will get you '/bar'.
Given http://example.com/bar/mumble the same code gets '/bar/mumble'. It's unclear from your question whether this is what you want. Nevertheless, you should at least start the parse as above.
The code i use for this is
request.getRequestDispatcher("jsp/caseconference.jsp").forward(request, response);
The code above works fine when i use send Redirect but in that part, I can't use request and response which gives an error.
Based on your comment above, I don't think you're getting an error, but rather, you want the last part of your url to go from LoginServlet to caseconference. You would have to create a url pattern for caseconference like this:
#WebServlet(
name = "LoginServlet",
description = "This is optional but helpful",
urlPatterns = "/caseconference"
)
public class LoginServlet extends HttpServlet {
....
}
Then create a variable in your doPost method to keep track of every path the user is on, for example:
String path = request.getServletPath();
Whenever the time comes for the user to redirect to roswellpark/caseconference set the path to caseconference:
path = "/caseconference";
Then, build your url like this, provided you have a folder in your directory named roswellpark or you make a url pattern for it:
String url = "roswellpark/" + path + ".jsp";
And lastly, forward the url using try/catch statements for best practice:
try{
request.getRequestDispatcher(url).forward(request,response);
}catch(Exception ex){
....
}
I would like to read from a servlet the exact URL that was set in the HTTP request. That is together with any URL rewritten parts (;jsessionid=…).
Is it possible?
You can get the request URL (the part before ; and ?) as follows:
StringBuffer requestURL = request.getRequestURL();
You can check as follows if the session ID was attached as URL path fragment:
if (request.isRequestedSessionIdFromURL()) {
requestURL.append(";jsessionid=").append(request.getSession().getId());
}
You can get and append the query string as follows, if any:
if (request.getQueryString() != null) {
requestURL.append('?').append(request.getQueryString());
}
Finally, get the full URL as follows:
String fullURL = requestURL.toString();
How can you access the REQUEST_URI from within a Struts 2 Action? In perl/php/ruby it is readily available via ENV["REQUEST_URI"] and the like. In java it seems that the PageContext.getErrorData().getRequestURI() is what I'm looking for, but sadly, the PageContext does not appear to be defined within the action, either because the ErrorDocument redirection makes the request not look like an error, or because it is defined later.
A particular example
Given an apache fronted (mod_jk/ajp) struts 2 app on tomcat reached via the ErrorDocument 404 configuration in apache. With the following details:
-- Original request url (which triggers the 404) --
http://server/totally/bogus/path
-- http.conf --
ErrorDocument 404 /struts2app/lookup.action
-- struts action --
public String bogusUrlLookup() {
HttpServletRequest request = ServletActionContext.getRequest();
// contains /lookup.action as does request.getRequestURI();
String url = RequestUtils.getServletPath(request);
// PageContext is null, so I cannot reach ErrorData from it.
log.info("pageContext="+ServletActionContext.getPageContext());
// Not in the ENV
// Map env = System.getenv();
// Not in the ATTRIBUTES
// request.getAttributeNames()
// Not in HEADER
// request.getHeaderNames()
return ERROR;
}
Again all I need is the string "/totally/bogus/path", but in the above action the only url string that I can find is "/struts2app/lookup.action". I'm tied to the ErrorDocument because the totally/bogus/path is not likely to be within the namespace of my application because apache serves other non-tomcat resources.
request.getAttribute("javax.servlet.forward.request_uri")
baseUri = (String)request.getAttribute("struts.request_uri");
Use:
JkEnvVar REDIRECT_URL ""
in your httpd.conf file. Then use request.getAttribute("REDIRECT_URL"); to get the variable in your jsp/servlets.
If you don't wanna miss the query string part, this is better:
final String referrer = (String) request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
if(referrer != null) {
final String query = (String) request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING);
if(query != null && query.length() > 0) {
url = referrer+ "?" + query;
}
else {
url = referrer;
}
// do something
}