Skip to Content
avatar image
Former Member

Is it possible to send data from Android to Sap Gateway ??? Problems with Token

Hi, I have created a SAP Gateway service that I have tested with the Sap Gateway Client and using from Chrome Postman and it´s working, but when I try to use the same with a Android app it doesn´t work, the Sap Gateway Service return 403, the problem it´s the token that I´m trying to recover with a Get request, I´m recovering in the Token the word "Requested", I have recovering the cookie, but the token it ´s impossible I´ve read this links about this topic.

https://archive.sap.com/discussions/thread/3185017 https://archive.sap.com/discussions/thread/3386003 https://help.sap.com/saphelp_nw74/helpdata/en/b3/5c22518bc72214e10000000a44176d/frameset.htm

https://archive.sap.com/discussions/thread/3674969

In brief, I want send a GET request to obtain the Token and the Cookie and late on it using in a POST request to insert the data using the SAP Gateway Service, the service it´s working properly, my problem is the recover the Token in a GET request.

Using Postman, I can recover it.

Any advice ?????

Thanks in advance..

This is the code than I´m using to trying it !!!

public String tokenAndSapSessionOdata(String aUrl){

String out = "";
try{
    		
URL url = new URL(aUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
String userpass = this.iOdata_user + ":" + this.iOdata_password;
String basicAuth = "Basic " + new String(Base64.encode(userpass.getBytes(), Base64.NO_WRAP));

conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", basicAuth);
conn.setRequestProperty("X-Requested-With" , "XMLHttpRequest");
conn.setRequestProperty("Content-Type", "application/atom+xml");
conn.setRequestProperty("X-CSRF-Token" , "Fetch");
conn.setRequestProperty("DataServiceVersion", "2.0");

String s = conn.getHeaderField("Set-Cookie");
System.out.println(s);

String d = conn.getHeaderField("x-csrf-token");
System.out.println(d);


Map<String, List<String>> map = conn.getHeaderFields();
List<String> listToken = new ArrayList<String>();
List<String> listCookie = new ArrayList<String>();       	

for (Map.Entry<String, List<String>> entry : map.entrySet()) {
        		
  if ("x-csrf-token".equals(entry.getKey())){
      listToken = entry.getValue();
  }
        		
  if ("set-cookie".equals(entry.getKey())){                  		          listCookie = entry.getValue();
  }
}


String aToken  = listToken.get(0);
String aCookie = listCookie.get(0);


out = aToken + "," + aCookie;
return out;	
    		
}catch(Exception e){
  System.out.println("Exception OdataGateway: "+e.getMessage());
  return "{}";
  	}
 }

out = aToken + "," + aCookie;
return out;	
    		
 }catch(Exception e){
  System.out.println("Exception OdataGateway: "+e.getMessage());
  return "{}";
 	}
 }

1.jpg (70.6 kB)
2.jpg (120.1 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Jul 20, 2017 at 12:58 PM

    I have found the solution for the obtain the Token

    https://help.hana.ondemand.com/hci_odata_provisioning/frameset.htm?d2e706bd63c043c7b7ae1d2dc830bb79.html

    First, I use a GET with Fetch to obtain it, and later on a POST with the Token, my problem SAP Gateway say "CSRF token validation failed"

    So I believe that Token has changed ....

    Any help ????

    Add comment
    10|10000 characters needed characters exceeded