I'm trying to obtain an eBay OAuth access & refresh token but keep getting a 401 Unauthorized response. I've been through every bit of documentation on this and tried just about everything but no joy.
I've been through the user permission flow, granted access to my application and have the authorize code - I'm manually pasting this into my code for now and have tried it both URL encoded and URL decoded but the same result.
I'm not sure if the problem lies with my java code or one of the values in my eBay developer account. Any ideas or pointers would be most welcome.
public int initialiseToken(String clientID, String clientSecret, String ruName)
{
int responseCode = 0;
try
{
String urlString = "https://api.ebay.com/identity/v1/oauth2/token";
String clientCredentials = clientID + ":" + clientSecret;
// base64 encode credentials
byte[] base64clientCredentials = Base64.encodeBase64(clientCredentials.getBytes());
// below authCode obtained from URI redirect following eBay auth sign-in
String authCodeURLEncoded = "v%5E1.1%23i%5E1%23I%5E3%23f.....xI0VeMjYw";
String authCodeURLDecoded = URLDecoder.decode(authCodeURLEncoded, "UTF-8");
URL url = new URL(urlString);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + base64clientCredentials);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Accept-Charset", "utf8");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("grant_type", "authorization_code");
conn.setRequestProperty("redirect_uri", ruName);
conn.setRequestProperty("code", authCodeURLDecoded); // have tried both encoded & decoded versions
String msg;
if (conn.getResponseCode() != 200)
{
responseCode = conn.getResponseCode();
msg = conn.getResponseMessage();
}
else
{
responseCode = conn.getResponseCode();
msg = conn.getResponseMessage();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String line = br.readLine();
parseResult(line);
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return responseCode;
}
I have a piece of code that's for OAuth Client credential ( for accessing general information) if it helps.
I am working on something very similar. I am right now stuck on using the returned access token to actually implement an ebay method. If you have managed to do that, can you let me know?
Dependency
implementation 'com.squareup.okhttp3:okhttp:3.5.0'
Code
public class MainActivity extends AppCompatActivity {
private static final String clientId = "-";//clientId
private static final String clientSecret = "-";//client secret
private static final String tokenUrl = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
private static final String auth = clientId + ":" + clientSecret;
private static final String authentication = Base64.encodeToString(auth.getBytes(),Base64.NO_WRAP);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.addHeader("Authorization", "Basic " + authentication)
.url(tokenUrl)
.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded")
,"grant_type=client_credentials"+
"&scope=https://api.ebay.com/oauth/api_scope"
)
)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
Log.d("Testing_", "Error: " +e.getMessage());
}
#Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
JSONObject data = null;
try {
data = new JSONObject(json);
String accessToken = data.optString("access_token");
String refreshToken = data.optString("refresh_token");
Log.d("Testing_", "Access Token = " + accessToken);
Log.d("Testing_", "Refresh Token = " + refreshToken);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
Output
Thanks Michael - will take a look at that when I pick this up again. In the meantime, here's some code that I've used to test the Search Cancellations interface - hopefully it may be of some use to you
public static void searchCancellations(String authToken)
{
String urlString = "https://api.ebay.com/post-order/v2/cancellation/search";
String urlStringParams = "?creation_date_range_from=2018-12-01T00:00:00.000Z&creation_date_range_to=2019-01-16T00:00:00.000Z";
try
{
String encodedUrl = urlString + URLEncoder.encode(urlStringParams, "UTF-8");
URL url = new URL(encodedUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Accept-Charset", "utf8");
conn.setRequestProperty("Authorization", "IAF " + authToken);
conn.setRequestProperty("Content-Type", "application/json");
int responseCode = 0;
String msg;
if (conn.getResponseCode() != 200)
{
responseCode = conn.getResponseCode();
msg = conn.getResponseMessage();
}
else
{
responseCode = conn.getResponseCode();
msg = conn.getResponseMessage();
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String line = br.readLine();
parseCancellationResult(line);
responseCode = 0;
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Related
I am a new user to the Spotify restful API. I have code that works for the restful calls I want to use, but cannot get the authorization to work through my Java client. The error message returned is:
400: Bad Request
{"error":"unsupported_grant_type","error_description":"grant_type parameter is missing"}
My code is:
public static void main(final String[] args) {
getToken();
System.out.println("<<< Finished >>>");
}
public static String getToken() {
final String payload = "grant_type: client_credentials";
final String unencodedCredential = CLIENT_ID + ':' + CLIENT_SECRET;
final String encodedCredential = Base64.getEncoder().encodeToString(unencodedCredential.getBytes());
String response = "";
try {
final URL url = new URL("https://accounts.spotify.com/api/token");
try {
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + encodedCredential);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("grant_type", "client_credentials");
connection.setRequestProperty("Content-Length", "" + payload.length());
connection.setDoInput(true);
try (final DataOutputStream writer = new DataOutputStream(connection.getOutputStream())) {
writer.writeChars(payload);
}
if (connection.getResponseCode() == 200) {
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
while (reader.ready()) {
System.out.println(reader.readLine());
}
}
} else {
System.out.println(connection.getResponseMessage());
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
while (reader.ready()) {
System.out.println(connection.getResponseCode() + ": " + connection.getResponseMessage());
System.out.println(reader.readLine());
}
}
}
} catch (final IOException x) {
x.printStackTrace();
}
} catch (final MalformedURLException x) {
x.printStackTrace();
}
return response;
}
I have a Jersey client that retrieves a jwt token from an API.
This is the code
public static final String INFO_ENDPOINT = "http://10.1.9.10:7100/Info/";
private static final String INFO_USERNAME = "user";
private static final String INFO_PASSWORD = "password";
private static final String AUTH_PATH = "auth";
private String token;
private final Client client;
public JerseyClient() {
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
client = Client.create(cc);
}
public void authenticate() {
try {
WebResource resource = client.resource(INFO_ENDPOINT + AUTH_PATH);
StringBuilder sb = new StringBuilder();
sb.append("{\"username\":\"" + INFO_USERNAME + "\",");
sb.append("\"password\":\"" + INFO_PASSWORD + "\"}");
ClientResponse clientResp = resource.type("application/json")
.post(ClientResponse.class, sb.toString());
String content = clientResp.getEntity(String.class);
System.out.println("Response:" + content);
token = content.substring(content.indexOf("\"token\":") + 9,
content.lastIndexOf("\""));
System.out.println("token " + token);
} catch (ClientHandlerException | UniformInterfaceException e) {
}
}
The above code returns a jwt token that is then used as a key for another call.
I am trying to convert it to use HttpUrlConnection. But that does not seem to work
This is what I have tried. It does not give an error, but does not return the token either. The response is empty
try {
URL url = new URL("http://10.1.9.10:7100/Info/auth");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("accept", "application/json");
String input = "{\"username\":user,\"password\":\"password\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (IOException e) {
}
My problem is I am not able to translate .post(ClientResponse.class, sb.toString()) to HttpUrlConnection.
What am I missing, or what am I doing wrong?
Thanks
The issue is resolved. The code works as is.
The problem was I was missing quotes for json value String
Should have been
String input = "{\"username\":\"user\",\"password\":\"password\"}";
I am implementing azure for my web application and trying to get access token by following there openId connect tutorial
https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code
And when i am requesting to get the access token, i am always getting bad request 400
Request to get access token :
POST /{tenant}/oauth2/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&client_id=2d4d11a2-f814-46a7-890a-274a72a7309e
&code=AwABAAAAvPM1KaPl.......
&redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F
&resource=https%3A%2F%2Fservice.contoso.com%2F
&client_secret=p#ssw0rd
here is my code :
public static String post( String endpoint,
Map<String, String> params) {//YD
StringBuffer paramString = new StringBuffer("");
//if(!Utilities.checkInternetConnection(context)){
// return XMLHandler.getXMLForErrorCode(context, JSONHandler.ERROR_CODE_INTERNET_CONNECTION);
//}
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
StringBuffer tempBuffer = new StringBuffer("");
String paramval;
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
if (param != null) {
if (paramString.length() > 0) {
paramString.append("&");
}
System.out.println( "post key : " + param.getKey());
String value;
try {
paramval = param.getValue();
if(paramval!=null)
value = URLEncoder.encode(paramval, "UTF-8");
else
value = "";
} catch (UnsupportedEncodingException e) {
value = "";
e.printStackTrace();
}
paramString.append(param.getKey()).append("=")
.append(value);
}
}
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(endpoint);
String data = "";
try {
// Add your data
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs))
//httppost.addHeader("Host", host);
httppost.addHeader("Content-Type",
"application/x-www-form-urlencoded");
if (!paramString.equals("")) {
if (tempBuffer.length() > 0) {
data = data + tempBuffer.toString();
}
data = data + paramString.toString();
if (data.endsWith("&")) {
data = data.substring(0, data.length() - 1);
}
httppost.setEntity(new ByteArrayEntity(data.getBytes()));
}
System.out.println( "post Stringbuffer : " + data);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int statuscode = response.getStatusLine().getStatusCode();
System.out.println("Response code : " + statuscode);
if (statuscode != 200) {
return null;
}
HttpEntity entity = response.getEntity();
InputStream in = null;
if (entity != null) {
in = entity.getContent();
}
if (in != null) {
StringBuilder builder = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, "UTF-8"));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} finally {
in.close();
}
String response2 = builder.toString();
System.out.println("response :" + response2);
retrycount = 0;
return response2;
}
}
catch(UnknownHostException e){
e.printStackTrace();
return null;
}
catch (EOFException eof) {
if (retrycount < max_retry) {
eof.printStackTrace();
post( endpoint, params);
retrycount = 1;
}
} catch (Throwable th) {
throw new IOException("Error in posting :" + th.getMessage());
}
retrycount = 0;
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Please help me with this
Thanks in Advance
Have you ensured the redirect uri passed to /token is the same as the one you passed to /authorize
I believe, it will help if you can test the OAuth auth code flow with your current client id, secret and scope using Postman tool in order to rule out bad configuration.
Please refer to the code below to request AuthorizationCode.
public static void getAuthorizationCode() throws IOException {
String encoding = "UTF-8";
String params = "client_id=" + clientId
+ "&response_type=" + reponseType
+ "&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F"
+ "&response_mode=query"
+ "&resource=https%3A%2F%2Fgraph.windows.net"
+ "&state=12345";
String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/authorize";
byte[] data = params.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5 * 1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
BufferedReader br = null;
if (conn.getResponseCode() != 200) {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
System.out.println("Response body : " + br.readLine());
}
Then you could get access token using the AuthorizationCode you got and get refresh code using the code below.
public static void getToken(String refreshToken) throws IOException {
String encoding = "UTF-8";
String params = "client_id=" + clientId + "&refresh_token=" + refreshToken
+ "&grant_type=refresh_token&resource=https%3A%2F%2Fgraph.windows.net";
String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
byte[] data = params.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5 * 1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
BufferedReader br = null;
if (conn.getResponseCode() != 200) {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
System.out.println("Response body : " + br.readLine());
}
Hope it helps you.
I've followed the tutorial here in an attempt to send data to an android device. I've whittled my Java class down to this:
public class App {
public static void main( String[] args ) {
try {
String apiKey = "api key generated in Google Developer Console...";
String deviceId = "Device Id generated, retrieved directly from logcat, as per tutorial...";
Content content = new Content();
content.addRegId(deviceId);
content.createData("Title", "Notification Message");
URL url = new URL("https://android.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + apiKey);
conn.setDoOutput(true);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
mapper.writeValue(wr, content);
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println(responseCode == 200 ? responseCode + ". This is the response we want..." : responseCode + ". This is not the response we want...");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
and this is my Content class:
public class Content implements Serializable{
private List<String> registration_ids;
private Map<String,String> data;
public void addRegId(String regId) {
if(registration_ids == null)
registration_ids = new LinkedList<String>();
registration_ids.add(regId);
}
public void createData(String title, String message) {
if (data == null)
data = new HashMap<String,String>();
data.put("title", title);
data.put("message", message);
}
}
I'm getting a 400 response code
java.io.IOException: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send
I'm sure I'm missing something small, but I can't see where it is.
I have used this tutorial to try to login to a website from the company I am doing my internship at: http://www.mkyong.com/java/how-to-automate-login-a-website-java-example/
However, It is not logging in, since the URL when you're logged in to the site is the same as when you're not logged in. So all that happens is me adding the data to the fields, and then the page refreshes, with nothing happening. Can anyone tell me how I am supposed to continue?
Thanks
(Edit): I only have a redirect link
public class ProfileLogin extends AsyncTask<Void, Void, Void>{
private List<String> cookies;
private HttpsURLConnection conn;
private final String USER_AGENT = "Mozilla/5.0";
private String page;
private String userName;
private String passWord;
private String postParams;
URL obj;
//Setting up out connection
public ProfileLogin(String user, String pass){
CookieManager cManager = new CookieManager();
CookieHandler.setDefault(cManager);
page = null;
try {
obj = new URL(LOGIN_URL);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Setting the username and password
userName = user;
passWord = pass;
try {
conn = (HttpsURLConnection) obj.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Starting our Asynctask to do all of the networking
execute();
}
public VpnProfile getProfiles(){
VpnProfile profile = new VpnProfile(null);
return profile;
}
public String getPageContent() throws Exception{
//Set Get method
conn.setRequestMethod("GET");
conn.setUseCaches(false);
//The properties of our site, we need to act like a browser
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4");
if(cookies != null){
for(String cookie : this.cookies){
conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
}
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + LOGIN_URL);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine = in.readLine()) != null){
response.append(inputLine);
}
in.close();
setCookies(conn.getHeaderFields().get("Set-Cookie"));
return response.toString();
}
private String getFormParams(String userName2, String passWord2) throws Exception{
System.out.println("Extract form's data...");
Document doc = Jsoup.parse(page);
//Elements of the login page
Element userNameElement = doc.getElementById("username");
Element passWElement = doc.getElementById("password");
List<String> paramList = new ArrayList<String>();
paramList.add(userNameElement.attr("name") + "=" + URLEncoder.encode(userName2, "UTF-8"));
paramList.add(passWElement.attr("name") + "=" + URLEncoder.encode(passWord2, "UTF-8"));
StringBuilder result = new StringBuilder();
for(String param : paramList){
if(result.length() == 0){
result.append(param);
} else {
result.append("&" + param);
}
}
return result.toString();
}
private void sendPost(String postParams) throws Exception {
conn = (HttpsURLConnection) obj.openConnection();
//Act like a browser
conn.setUseCaches(false);
conn.setRequestMethod("POST");
//This line is deleted, I can't show the url, I set the host here
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4");
if(cookies != null){
for(String cookie : this.cookies){
System.out.println("This is cookies: " + cookie);
conn.addRequestProperty("cookie", cookie.split(";", 1)[0]);
}
}
conn.setRequestProperty("Connection", "keep-alive");
//Deleted line setting referer URL
conn.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + LOGIN_URL);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine = in.readLine()) != null){
response.append(inputLine);
}
in.close();
}
#Override
protected Void doInBackground(Void... arg0) {
try {
page = getPageContent();
postParams = getFormParams(userName, passWord);
sendPost(postParams);
page = getPageContent();
Document doc = Jsoup.parse(page);
Element userNameElement = doc.getElementById("username");
if(userNameElement.toString() != null){
System.out.println("Not logged in");
}else{
System.out.println("Logged in!");
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
private void setCookies(List<String> cookies) {
this.cookies = cookies;
}
}
I kind of found a way around: How to log into Facebook programmatically using Java?
This works really well for me and gives me really clean and short code :)