everytime i want to send a push notification to my cell i get the 302 Moved Error from the Google servers. It says, it moved to www.google.com. This doesn't seem correct. So i have the following information:
I am sitting behind a proxy, but i tried this also without a proxy. No effect.
CUrl commandline method works fine
Receiving the auth token from the Google servers works fine
Changing the auth token i want to send has no real effect (changing the last 3-4 characters)
Sending no registration id has no effect
Here is the code, that sends a push notification:
QString headerStr = QString("GoogleLogin auth=") + m_authCode;
qDebug(qPrintable(headerStr));
QUrl url;
url.addQueryItem("registration_id", m_pRegCode->text());
url.addQueryItem("collapse_key", "0");
url.addQueryItem("data.message", "data");
qDebug("%s", qPrintable(url.toString()));
qDebug("%s", qPrintable(QString(url.encodedQuery())));
QByteArray data;
data = url.encodedQuery();
QUrl header("https://android.apis.google.com/c2dm/send");
QNetworkRequest req(header);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded;charset=UTF-8");
req.setHeader(QNetworkRequest::ContentLengthHeader, data.length());
req.setRawHeader(QByteArray("Authorization"), headerStr.toAscii());
qDebug("%s", qPrintable(req.url().toString()));
m_pPushRep = m_pManager->post(req, data);
The answer i receive is:
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
here.
</BODY></HTML>
What am i doing wrong?
Edit: The solution was actually pretty simple: I had a whitespace at the end of my authentication id. This actually led to the errorcode. So for everyone who reads this in the far future: CHECK YOUR AUTHENTICATION CODE!
I ran into a similar issue. Worked from cURL but not from Qt. I however had to use the -k flag with cURL aka ignore ssl errors. After I did a simple ignoreSslErrors() on the QNetworkReply* it worked just fine for me, this might do the trick for you too. I now have this code to do the api call.
QNetworkRequest request(QUrl("https://android.apis.google.com/c2dm/send"));
QUrl postData;
postData.addQueryItem("collapse_key","1");
postData.addQueryItem("registration_id",id);
postData.addQueryItem("data.message",message);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded;charset=UTF-8");
request.setHeader(QNetworkRequest::ContentLengthHeader, postData.encodedQuery().length());
request.setRawHeader("Authorization","GoogleLogin auth=....");
network->post(request,postData.encodedQuery())->ignoreSslErrors();
Which works just fine for me.
Related
When I send a POST fetch request to my website to login it all works on iOS. The fetch response set-cookie have all the cookies I need to proceed and make future requests. The problem is on Android I only receive one cookie in set-cookie, even though there is more. It seems like it gets cut and I can't seem to access the raw response.
My fetch code:
const myRequest = new Request('MYURL',
{method: 'POST', body: parameters,
headers: {
"Referer": 'MYURL.com/login',
'Origin': 'MYURL',
'Content-Type': 'application/x-www-form-urlencoded'
}});
fetch(myRequest)
.then(
function(response) {
try {
console.log("JSON RESPONSE: " + response.json());
console.log(JSON.stringify(response, null, 2));
console.log(response.headers.get('Content-Type'));
console.log(response.headers.get('Date'));
} catch(err) {
console.log("ERROR: " + err);
}
}
)
.catch(function(err) {
console.log('Fetch Error', err);
});
I know that android gets correctly logged in cause the server redirects me to the menu page and also recognizes the clients personal ID.
Here is the response from iOS(the one that works):
"set-cookie": "session=CENSORED; Domain=.MYURL; Path=/; HttpOnly, clientID=123; expires=Thu, 07-Nov-2019 09:27:20 GMT; domain=.MYURL; path=/",
Perfect response. Everything works.
Here is the response from android:
"set-cookie": "clientID=123; expires=Thu, 07-Nov-2019 09:34:00 GMT; domain=.MYURL; path=/",
I've tried for several days to understand the problem but to no avail. I've tried to test cookies without httponly to see if that was the error but no.
I hope some experienced react-native users can shine light on this problem. I haven't experienced it before when I created native apps for android and IOS. Only with react-native. :-(
I hope to get to this to work since I would rather avoid using native code for this small part.
EDIT: Tested fetch function with reddit login api and same problem occurs. Only 1 cookie in response set-cookio on Android but all cookies shown on iOS.
Look into the official documents of React Native and MDN:
https://facebook.github.io/react-native/docs/network
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
The problem here is fetch won't send or receive any cookie from server by default. So you have to enable it by add credentials. And there are some libraries that can help you manage cookies:
https://github.com/joeferraro/react-native-cookies
Let me start by saying I've found several proposed solutions online, but none of them seem to work for me.
Issue:
I have a meteor app I'm trying to run on android. For this, I've deployed the app on Heroku and I call the run android-device command using the --mobile-server https://myapp.heroku.com parameter.
I permanently receive the error
"XMLHttpRequest cannot load https://myapp.heroku.com/sockjs/... . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:12848' is therefore not allowed access. The response had HTTP status code 404.", source: http://localhost:12848/ (0)
Here is what I've tried so far:
I set ROOT URL at meteor startup:
process.env.ROOT_URL = "https://myapp.heroku.com";
I tried setting the Access Control like this, server-side at meteor startup:
WebApp.connectHandlers.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header('Access-Control-Allow-Origin', 'https://myapp.heroku.com');
res.header('Access-Control-Allow-Origin', 'http://localhost:12848');
res.header('Access-Control-Allow-Origin', 'http://meteor.local');
res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Custom-Header");
return next();
});
I tried to use the browser-policy package, like this, server-side at meteor startup:
BrowserPolicy.content.allowSameOriginForAll();
BrowserPolicy.content.allowOriginForAll('*');
BrowserPolicy.content.allowOriginForAll('http://meteor.local');
BrowserPolicy.content.allowOriginForAll('https://myapp.heroku.com');
BrowserPolicy.content.allowOriginForAll('https://*.myapp.heroku.com');
BrowserPolicy.content.allowEval();
I tried adding access rules to "mobile-config.js":
App.accessRule("*");
I made sure the name in the "package.json" file under root is identical to the App name under "mobile-config.js"
What else am I missing?
Edit:
I've also tried adding the express and cors packages to whitelist local host:
var whitelist = [
'http://localhost:3000',
'http://localhost:12848',
'https://myapp.heroku.com'
];
var corsOptions = {
origin: function(origin, callback){
var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
callback(null, originIsWhitelisted);
},
credentials: true
};
app.use(cors(corsOptions));
Also tried to enable pre-flight, like this:
app.options('*', cors())
This is probably the stupidest problem I ever ran into. Trying to run the app using the --mobile-server https://myapp.heroku.com parameter was wrong. Instead, it should be https://myapp.herokuapp.com
That was it. That was the problem all along...
Adding '*' to whitelist should do the job. The ultimate solution lies within config.xml, this should be helpful: https://stackoverflow.com/a/36124935/8056323
My code used to work, it does not work anymore, I tried troubleshooting and can't figure out why.
I have this piece of code in my PHP:
$android_id_01 = $_GET['pmysql_room_id'];
$android_id_02 = "";
$f = fopen("00_android_id_01.txt", "w");
fwrite($f, print_r($android_id_01, true));
fclose($f);
$f = fopen("00_android_id_02.txt", "w");
fwrite($f, print_r($android_id_02, true));
fclose($f);
For troubleshooting I created two android IDs ($android_id_01 and $android_id_02) which are both empty (The first one is From Android and the second one I created directly from PHP).
Now when I launch my Android device, the PHP file is executed from server side and both the text files are created empty and identical. Now my code only works when I use $android_id_02 and not $android_id_01 from the code below:
if ($android_id == '')
{
//my code
}
(Yes when I use either one of the $android_id_01 OR $android_id_02 I rename it to $android_id and comment out the other one)
My question is, although this was working yesterday, why does it work with $android_id_02 = ""; and not $android_id_01 = $_GET['pmysql_room_id']; even though they are both empty????
I don't know what changed from yesterday to today.
Ok after a bit of troubleshooting I found a solution, strange though.
On the server side "display_errors" under PHP settings must be turned off. Somehow having this on interferes with the json_encode sent back to android client. (even though my code is not generating any errors)
I'm building an Application that integrates with your Nest devices (both the thermostat and the Nest Protect, but this issue is about the thermostat).
What I'm trying to do is set my thermostat's ETA to be in x minutes (2 hours for example so 120 minutes).
This is my code that I'm executing:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
final String path = buildStructureFieldPath(structureID, Keys.STRUCTURE.ETA);
Structure.ETA eta = new Structure.ETA.Builder()
.setTripID(tripId)
.setEstimatedArrivalWindowBegin(sdf.format(estimatedArrivalBegin))
.setEstimatedArrivalWindowEnd(sdf.format(estimatedArrivalEnd))
.build();
sendRequest(path, eta.toJSON().toString(), listener);
The path is /structures/MY_STRUCTURE_ID/eta
Unfortunately that's not working. I'm always getting an error code -2 and error message: No write permission(s) for field(s): eta
And that's were it gets strange. No permission, but I did request the permission and I did an authenticate, which is successful, before launching the update call.
In the two attached screenshots you can see first my Nest Developer Account where you can find the ETA write permission and in the second you can see the logging from within my app (using the NestAPI as can be found on GitHub, just added the ETA feature myself).
Anyone have any idea on how to solve this issue?
Can you print out the exact JSON blob you're sending and post it here? (the value of eta.toJSON().toString())
Best guess is that it isn't formatted exactly correctly and as such is maybe attempting to write in such a way that doesn't adhere to the api-reference.
This is the format that it needs to match:
"eta": {
"trip_id": "myTripHome1024" ,
"estimated_arrival_window_begin": "2015-10-31T22:42:59.000Z" ,
"estimated_arrival_window_end": "2015-10-31T23:59:59.000Z"
}
Single line:
{"eta":{"trip_id":"myTripHome1024","estimated_arrival_window_begin":"2015-10-31T22:42:59.000Z","estimated_arrival_window_end": "2015-10-31T23:59:59.000Z"}}
To pinpoint exactly which field may be erroneous, try sending just one change at a time for each ie: structures/ID/eta/trip_id, etc for the others.
Useful JSON Validator: http://jsonlint.com/
You could also try to send it to /structures/MY_STRUCTURE_ID.json?auth=[TOKEN] instead of /structures/MY_STRUCTURE_ID/eta.
I can't seem to figure this one out, and I've tried googling that my fingers now hurt.
I am performing a HTTPClient request to my Domino Server that has SSL enabled. My request works perfectly when testing on iOS, but fails every time when testing through the Android Emulator and Mobile Web.
I'm running Android 2.2 SDK.
When I try to sign in from the App, I am definitely reaching the Server, because HTML is returned (the Login Web Form). When HTML is returned, it either means that the Authentication failed, or that the Redirect didn't work. When signing in through iOS the page redirects 100%.
Below is my code:
var orderReq = Titanium.Network.createHTTPClient();
var myurl = 'https://domain/db.nsf?login';
orderReq.setEnableKeepAlive;
orderReq.open('POST',myurl, false);
var params = {
username: "Joe Smith",
password: "Password",
redirectto: "path/db.nsf/response.xsp"
};
orderReq.send(params);
var myreturn = orderReq.responseText;
if((myreturn.charAt(0) === '<') || (myreturn === ""))
{
Ti.API.info('Fail');
return 'Fail';
}
else
{
Ti.API.info('Pass');
var json = orderReq.responseText;
var response = eval('(' + json + ')');
return response.username;
}
I have tried many properties and to no avail. I can confirm that the Android Emulator can connect to the Internet. It feels like either the Parameters are not being passed or the Redirect is not being triggered on the Web Page.
NOTE: I notice when authenticating through Android emulator that it fails immediately, where iOS returns true or false after 1-2 seconds.
Okay i know the question is too old. But, for anyone who is looking for an answer, this is what worked for me.
Make sure params is stringified before sending it to the server
orderReq.send(JSON.stringify(params));
Include the following right after the lines - orderReq.open('POST',myurl, false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('charset','utf-8');