【问题标题】:Windows Notifications Service: 401 Invalid Token when trying to create a Toast notification in PHPWindows 通知服务:尝试在 PHP 中创建 Toast 通知时出现 401 Invalid Token
【发布时间】:2015-01-30 19:29:36
【问题描述】:

我一直在尝试向模拟器发送 Toast 通知。 我创建了 Windows Phone 8.1 应用程序并将其与商店中的应用程序相关联。 之后我设法获得了我必须用来调用通道 URI 的访问令牌。

当我尝试使用通道 URI 和访问令牌发送 Toast 通知时,我收到此错误。

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

这是我创建的php测试代码。

<?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
?>  

在 c# 中,我使用它来获取频道 URI。

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

知道什么会导致这个问题吗?

【问题讨论】:

标签: c# php windows-phone-8 push-notification


【解决方案1】:

我找到了解决办法。

您必须发送以请求令牌的 client_id 不是您应用的客户端 ID。 您必须发送包 SID,而不是发送客户端 ID。 非常混乱。

【讨论】:

  • 还有一点要补充。您需要 整个 包 SID。这包括协议“ms-app://”。
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多