401 error while sending messages via HTTP
Hello,
I am getting 401 error while sending messages via HTTP POST. Below are the steps I have followed as per the doc https://github.com/SAP/iot-starterkit
1. Deploy MMS
2. Assign IoT-MMS-user role to my user
3. Under authentication tab, checked the ‘user and password’ selection (required to get response as success message instead of login page)
4. Created Device type, devices and message types
5. Using the url https://iotmmsd063183trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/[device id] to send message
The application I am using is iOS native application. Below is the code snippet.
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString: @”https://iotmmsd063183trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/[deviceid]“] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@”POST”];
[request setValue:@”application/json” forHTTPHeaderField:@”Content-Type”];
[request setHTTPBody:postData];
NSString* token = @”[MyToken]”;
NSData* bearerToken = [token dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@”Bearer %@”, [bearerToken base64EncodedDataWithOptions:4]];
[request setValue:authValue forHTTPHeaderField:@”Authorization: %@”];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Please tell me what I am doing wrong here.
Thanks,
Prathvi
Hi Prathvi,
it seems that you are setting the header field for the OAuth token not correctly. Furthermore, the content type should be different. Try to use the following:
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"Bearer %@", [MyToken]] forHTTPHeaderField:@"Authorization"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long) [postData length]] forHTTPHeaderField:@"Content-Length"];
Just let me know if this works.
Best regards,
Michael
Hi Michael,
I am unable to build the project if i send the token without encoding. It says expected identifier after [MyToken]. I am using xcode version 6.2
Thanks,
Prathvi
Hi Prathvi,
Please ensure that the OAuth token ([MyToken]) is of type NSString. Maybe you try first to add the token hardcoded, e.g.:
[request setValue:[NSString stringWithFormat:@"Bearer %@", @"aba0f9e6baf8e429ea336aa2e68569a"] forHTTPHeaderField:@"Authorization"];
Best regards,
Michael
Hi Michael,
Yes, absolutely correct. I am able to send data successfully.
Thanks for your time and consideration.
Regards,
Prathvi
Hi Michael,
Is it possible to do HTTP GET request for all the devices which belongs to one device type at a time.
Thanks,
Prathvi
Hi Prathvi,
I´m not sure if I understood your question right. Do you want to have a list of all devices belonging to one device type?
All supported requests can be found here: SAP HANA Cloud Platform Internet of Things (IoT) Services (beta)
Best regards,
Michael
Hi Michael,
I have a message type of id 1 and I have 3 fields for this message type. Now I want to read all data of message type(id=1) which belongs to a particular device type(id=1)
Is this the GET url? https://iotmmsd063183trial.hanatrial.ondemand.com/com.sap.iotservices.mms/api/messagetypes?devicetype=<id>&messagetype=<id>
And is it possible to test this url in browser. There is no sample HTTP client for GET request.
Thanks,
Prathvi
Hi Prathvi,
MMS does not provide any API to retrieve data sent by devices and stored in SQL. What you would need to do is to access the data by reading directly from the DB.
SAP HANA Cloud Platform Internet of Things (IoT) Services (beta)
BR,
Thomas
Hi Michael,
I am trying to use send data using the hcp-iot-api using node.js and following is the code :
var API = require("hcp-iot-api");
var mms = new API.MessageManagementService({
"account": "<username>", // This one is mandatory!
"password": "<password>",
"deviceToken": "<deviceToken>",
"deviceId": "<deviceId>",
"oauthToken": "<oauthToken>"});
mms.sendData({
"messageType": "<messagetype_id>",
"messages": [
{
"sensor1": "Value 1",
"sensor2": "Value 2" }
]
})
.catch(function(err) { console.log(err.message); });
I have registered the device, created message type and device type using the IoT cockpit.
I get error
Error "Access to the requested resource is not authorized (HTTP 401)
Please guide me!!
Hi Tushar,
for "sending data from device" case HTTP 401 would mean that your request misses or uses incorrect authorisation HTTP header. Make sure, you set one in your implementation with the name "Authorization" and value "Bearer <oauthtoken>".
Please, check with online doc for Data Services API Authentication SAP HANA Cloud Platform Internet of Things Services
Regards,
Anton
do you get the same error if you use the test tool provided by the service?
check this post - i was able to get a successful message from the test tool
my first use of HCPtrial IoT Service