Cannot send iOS push notifications - 400 BadDeviceToken response - apple-push-notifications

I have generated the .pem files for development using these commands in the Mac terminal:
openssl x509 -inform der -outform pem -in aps_development.cer -out PushNotificationAppCertificate.pem
openssl pkcs12 -in PushNotification.p12 -out PushNotificationAppKey.pem -nocerts
cat PushNotificationAppCertificate.pem PushNotificationAppKey.pem > PushNotificationAppCertificateKey.pem
And on the server I use PHP:
$curl = curl_init();
$device_token = "oVVEyzOm8LYn1nEYQLUcp1nBQw+UKHUeJbVHxcqr3ls=";
$pem_file = "PushNotificationAppCertificateKey.pem";
$pem_secret = "<password>";
$apns_topic = "<myapp.id>";
$sample_alert = '{"aps":{"alert":"hi","sound":"default"}}';
$url = "https://api.development.push.apple.com/3/device/".urlencode($device_token);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
However, BadDeviceToken is the response, and I am not getting the notification on the device.
According to the Apple documentation, either the device token is invalid, or I am using production certificate for the development server or vice versa; neither of which are the case here.
How can I make this work?

I solved it.
The problem was with the device token - if you knew, how they should look like, you would instantly tell it was not acquired the right way. It is not supposed to contain = or + characters, and its length should be 64 characters.
In my RegisteredForRemoteNotifications() function (Xamarin.iOS) I had used the
deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None)
function to get the token in string form. But the correct procedure is
byte[] bytes = deviceToken.ToArray<byte>();
string[] hexArray = bytes.Select(b => b.ToString("x2")).ToArray();
string tokenString = string.Join(string.Empty, hexArray);

Related

how to work locally with laravel when third-party api need only production IP to whitelist?

The scenario like this, I deployed my laravel app to heroku. The third-party api needs my production IP for whitelisting in order to use their resources. Heroku doesn't provide ip, I use add on to get ip. It connected ok between my app to third-party now. The problem is that, when I clone my app to local and start to run and test locally, it give error. (Undefined index: host) My code is below
$testparam = 'q='.$q.'&s='.$s;
$quotaguard_env = getenv("QUOTAGUARDSTATIC_URL");
$quotaguard = parse_url($quotaguard_env);
$proxyUrl = $quotaguard['host'].":".$quotaguard['port'];
$proxyAuth = $quotaguard['user'].":".$quotaguard['pass'];
$url = "http://xxxxx.com/api/resource";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxyUrl);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
curl_setopt($ch, CURLOPT_POSTFIELDS,$testparam);
$response = curl_exec($ch);
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$array = json_
decode($json,TRUE);
dd($array);
Currently, I have to push my file to heroku after I write one or two lines of code in order to see the result. Is there any way to write and test locally?
You need to set the QUOTAGUARDSTATIC_URL environment variable to what is in the QuotaGuard Admin dashboard or in Heroku's heroku config -a APPNAME command.

I'm using Apple Pay JS to integrate it into my website what should I sent onMerchantValidate

what I know so far is that I have to send a POST request to the apple server.
How or where can I get the merchIdentityCert cert and key??
NOTE: I'm going to send the post request from PHP
const options= {
url: endpointURL,
cert: merchIdentityCert,
key: merchIdentityCert,
method: 'post',
body:{
merchantIdentifier: "merchant.com.example.mystore",
displayName: "MyStore",
initiative: "web",
initiativeContext: "mystore.example.com"
},
json: true,
}
I figured how to connect to apple Servers and create the needed certificates for the POST request to work.
These steps must be done from a mac
Go to Apple Developer -> Create Certificate -> Apple Pay Merchant Identity
From your Mac go to KeyChain Access-> On the top right Select Keychain Access-> Certificate Assistant -> Request a Certificate From a Certificate Authority
Select Save to Disk & Let me specify key pair information
Key Size = 2048 bits / Algorithm = RSA
Upload the CSR to your Apple Pay Merchant Identity
Download the certificate given by Apple Pay and upload it to your Keychain Access
Export the certificate from Keychain Access as .p12 format
Open terminal direct it to the folder with the exported .p12 certificate
Run these commands
"openssl pkcs12 -in ApplePayMerchantIdentity_and_privatekey.p12 -out ApplePay.crt.pem -clcerts -nokeys"
"openssl pkcs12 -in ApplePayMerchantIdentity_and_privatekey.p12 -out ApplePay.key.pem -nocerts"
$path = "yourPath/ApplePay.crt.pem";
$path_key = "yourPath/ApplePay.key.pem";
// create a new cURL resource
$ch = curl_init();
$data = array(
"merchantIdentifier" => "Your merchant Name",
"displayName" => "Your Shop Name",
"initiative" => "web", // If you're going to use it on the web
"initiativeContext" => "Domain"
);
$data = json_encode($data);
$header = array(
'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLCERT, $path);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, ""); // For the Certificate Password
curl_setopt($ch, CURLOPT_SSLKEY, $path_key);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$serverOutput = curl_exec($ch);

HTTP Session from service cloud using PHP code

Is it possible to send custom XML messages from oracle service cloud while maintaining HTTP session?
So far I've managed to send a single message using cURL:
<?php
use \Rightnow\Connect\v1_2 as RNCPHP;
use \Rightnow\CPM\v1 as RNCPM;
$url1 = "";
$startInterviewHeaders = array("SOAPAction: http://oracle");
$startInterview;
// session
//$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
if(!function_exists("\curl_init")){
\load_curl();
echo "curl loaded";
} else {
echo "curl already exists ";
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $startInterview);
curl_setopt($ch, CURLOPT_HTTPHEADER, $startInterviewHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$startInterviewresponse = curl_exec($ch);
//echo $response;
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errno= curl_errno($ch);
echo "</br> HTTP status: " . $http_status . "</br> cURL error: " .$curl_errno . "</br>";
curl_close($ch); // close cURL
echo $startInterviewresponse;
?>
EDIT: Code above sends a single message and gets a response, but when I try to maintain HTTP session with CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE, second cURL message response complains that there is no active session.
sessions.com.oracle.determinations.server.exceptions.NoActiveInterviewExceptionaction "Investigate" can not be performed without an active interview
Code I used to maintain session (worked when testing outside Rightnow environment)
curl_setopt($ch,CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems
Thanks!
Your code sample is a Custom Process Model. CPMs do not allow persistence and will be closed once the CPM completes. You can run multiple curl calls from one CPM, but that is not recommended; you should use an integration middleware if you need multiple calls per CPM.
As long as you're running this code from an asynchronous CPM (synchronous CPMs don't expose curl, so that should be the case here), then an error on the OSvC side is likely an issue connecting to your "local test server", which is almost always not exposed to the public internet in an enterprise environment. Therefore, your "crash" is likely a connection error.
Per #drew010, you need to include your error in the context of this question as well.

Piping emails from postfix under Plesk to Ostickets helpdesk

I'm trying to pipe support emails from my production mail server (Plesk 12 and Postfix) to send them to another VPS hosting my helpdesk installation, I have chosen ostickets helpdesk for many reasons (Open source,PHP/MySQL...)
So, the idea is to trigger email reception on some adresses such as contact#company.com, support#company.com... then resend them to ostickets via its API in order to create a new ticket there.
I tried this way
http://blog.absolutedisaster.co.uk/osticket-plesk-9-postfix-pipe-mail-to-a-progr/
It explains how to create a pipe to trigger email, lunch php script ...etc
Because of some permission issues, I kept all this configuration and change just the last thing : php script
So I've replaced this php script by a simple bash script doing the same thing : send a request to API to create a new ticket via CURL.
Now In my production mail server the pipe is recognized and the email is trigged successfully :
The same in the other side, osticket API is receiving the request:
The problem is that the http response is 400 as you can see, which means ans error code #66 in ostickets (malformed email).
So, I think the problem is in my bash script :
exec curl --header 'X-API-Key: API_KEY_HERE' --user-agent 'osTicket API Client v1.7' --data - 'http://support.company.com/api/tickets.email'
the original PHP script I have replaced by this is :
#!/usr/bin/php -q
<?php
# Configuration: Enter the url and key. That is it.
# url => URL to api/tickets.email e.g http://yourdomain.com/support/api/tickets.email
# key => API's Key (see admin panel on how to generate a key)
#
$config = array(
'url'=>'http://support.company.com/api/tickets.email',
'key'=>'API_KEY_HERE'
);
#pre-checks
function_exists('file_get_contents') or die('upgrade php >=4.3');
function_exists('curl_version') or die('CURL support required');
#read stdin (piped email)
$data=file_get_contents('php://stdin') or die('Error reading stdin. No message');
#set timeout
set_time_limit(10);
#curl post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['url']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result=curl_exec($ch);
curl_close($ch);
//Use postfix exit codes...expected by MTA.
$code = 75;
if(preg_match('/HTTP\/.* ([0-9]+) .*/', $result, $status)) {
switch($status[1]) {
case 201: //Success
$code = 0;
break;
case 400:
$code = 66;
break;
case 401: /* permission denied */
case 403:
$code = 77;
break;
case 415:
case 416:
case 417:
case 501:
$code = 65;
break;
case 503:
$code = 69;
break;
case 500: //Server error.
default: //Temp (unknown) failure - retry
$code = 75;
}
}
exit($code);
?>
What is missing in my bash script ? (especially the stdin input)
thank you so much,
UPDATE
The problem was indeed in the bash script, here is the solution I came up with:
#!/bin/bash
data=`cat -`
exec curl --header 'X-API-Key: API_KEY_HERE' --user-agent 'osTicket API Client v1.7' --data "$data" 'http://support.company.com/api/tickets.email'
The problem was indeed in the bash script, here is the solution I came up with:
#!/bin/bash
data=`cat -`
exec curl --header 'X-API-Key: API_KEY_HERE' --user-agent 'osTicket API Client v1.7' --data "$data" 'http://support.company.com/api/tickets.email'

Grab current session's cookie with cURL

I'm working on a script that send a POST request to an URL, actually I'm sending the cookies manually in the header.. but how to take it from the current browser session?
I used tcpdump and grep but it's really the wrong choice :,D
Some suggestions?
I wont take them from a file but from the browser session and without enter in the cookie path of the browser
Curl can handle that for you; there is an option to store the cookies in a cookiejar and use those in subsequent requests.
Here is an example from the main curl site, which uses cookies from a file cookies.txt to set some and at the same time stores new cookies in newcookies.txt.
http://curl.haxx.se/docs/httpscripting.html#Cookie_Basics
curl --cookie cookies.txt --cookie-jar newcookies.txt http://www.example.com
When going through a login process, for example, one would reuse the cookies from cookie jar.
I had found a link now broken with an example from snipplr.com. I'll post it here for future references (credits to the owner):
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (isset($_COOKIE[session_name()])) {
curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.$_COOKIE[session_name()].'; path=/');
}
session_write_close();
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'curl error: '.curl_error($ch);
}
curl_close($ch);
session_start();

Resources