1

I have been trying to send a Toast notification to a simulator. I created the Windows Phone 8.1 app and Associated it with the app in the store. Afterwards I managed to get the access token that I have to use to call the channel URI.

When I try to send a Toast Notification using the channel URI and access token I get this error.

Bearer error="invalid_request",error_description="Invalid token"

This is the php test code that I created.

<?php

    //GET ACCESS TOKEN
    $tokenRequest = curl_init();
    curl_setopt($tokenRequest, CURLOPT_URL, 'https://login.live.com/accesstoken.srf');

    curl_setopt($tokenRequest, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    ));

    //FIELDS
    $fields = array(
        'grant_type' => 'client_credentials',
        'client_id' => '0',
        'client_secret' => 'Q',
        'scope' => 'notify.windows.com'
    );

    $fields_string = "";
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    curl_setopt($tokenRequest, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($tokenRequest,CURLOPT_POST, count($fields));
    curl_setopt($tokenRequest,CURLOPT_POSTFIELDS, $fields_string);

    $output = json_decode(curl_exec($tokenRequest));
    curl_close($tokenRequest);
    echo "<br>";
    var_dump($output);
    echo "<br>";
    $accessToken = $output->{'access_token'};


    //SEND PUSH
    $sendPush = curl_init();
    curl_setopt($sendPush, CURLOPT_URL, 'https://db3.notify.windows.com/?token=AwYAAABuWLaYT1f9BVJwNJCjc243U1OFXUT8MCqvsME%2ftDnhPG%2f%2fJSurxP3u1y47eqmrQZSPUlZH7koHW3Zwdj5938LYZNRdDyE6JzvyHOZvZvSo%3d');

    //TOAST MESSAGE
    $toastMessage = "<wp:Notification xmlns:wp=\"WPNotification\">" .
            "<wp:Toast>" .
            "<wp:Text1>" . "SendToast" . "</wp:Text1>" .
            "<wp:Text2>" . "Text Message" . "</wp:Text2>" .
            "</wp:Toast> " .
            "</wp:Notification>"; 

    curl_setopt($sendPush, CURLOPT_HEADER, true);

    $headers = array('Content-Type: text/xml', "Content-Length: " . strlen($toastMessage), "X-WNS-Type: wns/toast", "Authorization: Bearer $accessToken");
    curl_setopt($sendPush, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($sendPush, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($sendPush,CURLOPT_POST, 1);        
    curl_setopt($sendPush, CURLOPT_POSTFIELDS, $toastMessage);         

    $output = curl_exec($sendPush);

    echo "<br>";
    var_dump(curl_getinfo($sendPush, CURLINFO_HTTP_CODE));
    echo "<br>";
    var_dump(curl_getinfo($sendPush, CURLINFO_HEADER_OUT));
    echo "<br>";
    var_dump($output);
    curl_close($sendPush);
    // Create request to send
?>  

In c# I use this to get the channel URI.

 channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
                Debug.WriteLine(channel.Uri);

Any idea what could cause this problem?

Rudy
  • 91
  • 4
  • The 401 usually means the server has not been authorized or the client secret is wrong. I don't know CURL but you can compare your logic with mine in .NET by following this walkthrough: http://blogs.msdn.com/b/wsdevsol/archive/2012/10/04/walkthrough-creating-an-iis-server-to-use-with-wns-push-notifications-and-windows-store-apps.aspx – Jeff Sanders - MSFT Dec 02 '14 at 21:24

1 Answers1

8

I found the solution.

The client_id that you have to send to request the token isn't the Client ID of your app. Instead of sending the Client ID you have to send the Package SID. Very confusing.

Rudy
  • 91
  • 4