Php 401授权错误Windows Phone 8通知

Php 401授权错误Windows Phone 8通知,php,vb.net,windows-phone-8,push-notification,Php,Vb.net,Windows Phone 8,Push Notification,我正在尝试使用经过身份验证的服务器向我的Windows Phone 8应用程序发送通知,但每当尝试发送通知时,我都会收到401错误 我一直在遵循MSDN页面上的说明,也就是说,必须将TLS证书的密钥使用值设置为包含客户端身份验证。我不知道这意味着什么,在线搜索也不会给我任何线索 这可能是不正确的,也可能是我的代码,如下所示 VB.NET代码: Private Async Sub ApplicationBarPin_Click(sender As Object, e As EventArgs)

我正在尝试使用经过身份验证的服务器向我的Windows Phone 8应用程序发送通知,但每当尝试发送通知时,我都会收到401错误

我一直在遵循MSDN页面上的说明,也就是说,必须将TLS证书的密钥使用值设置为包含客户端身份验证。我不知道这意味着什么,在线搜索也不会给我任何线索

这可能是不正确的,也可能是我的代码,如下所示

VB.NET代码:

Private Async Sub ApplicationBarPin_Click(sender As Object, e As EventArgs)
    ' Holds the push channel that is created or found.
            Dim pushChannel As HttpNotificationChannel

            ' The name of our push channel.
            Dim channelName As String = "WindowsPhoneTrainTileNotification"

            ' Try to find the push channel.
            pushChannel = HttpNotificationChannel.Find(channelName)

            ' If the channel was not found, then create a new connection to the push service.
            If pushChannel Is Nothing Then
                pushChannel = New HttpNotificationChannel(channelName, "redsquirrelsoftware.co.uk")
                uri_timer = New DispatcherTimer
                uri_timer.Interval = TimeSpan.FromSeconds(5)
                AddHandler uri_timer.Tick, AddressOf UriTimerTick
                uri_timer.Start()

                ' Register for all the events before attempting to open the channel.
                AddHandler pushChannel.ChannelUriUpdated, AddressOf PushChannel_TileChannelUriUpdated
                AddHandler pushChannel.ErrorOccurred, AddressOf PushChannel_TileErrorOccurred

                pushChannel.Open()

                pushChannel.BindToShellTile()
            Else
                ' The channel was already open, so just register for all the events.
                AddHandler pushChannel.ChannelUriUpdated, AddressOf PushChannel_TileChannelUriUpdated
                AddHandler pushChannel.ErrorOccurred, AddressOf PushChannel_TileErrorOccurred

                Dim form As New MultipartFormDataContent()
                form.Add(New StringContent(Statics.getUserID), "userId")
                form.Add(New StringContent(pushChannel.ChannelUri.ToString()), "uri")
                form.Add(New StringContent(Statics.CurrentScheduleId), "scheduleId")

                Dim httpClient As HttpClient = New HttpClient()

                Dim response As HttpResponseMessage = Await httpClient.PostAsync("http://redsquirrelsoftware.co.uk/trains/push/WPTileSubscribe.php", form)
            End If

            ShellTile.Create(New Uri("/Route.xaml?scheduleId=" & scheduleId, UriKind.Relative), secondaryTile, True) 'Create SecondaryTile and pass querystring to navigation url.
        Catch ex As Exception
        End Try

    End If
End Sub

Private Async Sub PushChannel_TileChannelUriUpdated(sender As Object, e As NotificationChannelUriEventArgs)
    Dim form As New MultipartFormDataContent()
    form.Add(New StringContent(Statics.getUserID), "userId")
    form.Add(New StringContent(e.ChannelUri.ToString()), "uri")
    form.Add(New StringContent(Statics.CurrentScheduleId), "scheduleId")

    Dim httpClient As HttpClient = New HttpClient()

    Dim response As HttpResponseMessage = Await httpClient.PostAsync("http://redsquirrelsoftware.co.uk/trains/push/WPTileSubscribe.php", form)
End Sub

Private Sub PushChannel_TileErrorOccurred(sender As Object, e As NotificationChannelErrorEventArgs)
    MessageBox.Show("Error creating live tile, please try again")
End Sub

Private Async Sub UriTimerTick(sender As Object, e As EventArgs)
    Try
        If pushChannel.ChannelUri IsNot Nothing Then
            Dim form As New MultipartFormDataContent()
            form.Add(New StringContent(Statics.getUserID), "userId")
            form.Add(New StringContent(pushChannel.ChannelUri.ToString()), "uri")
            form.Add(New StringContent(Statics.CurrentScheduleId), "scheduleId")

            Dim httpClient As HttpClient = New HttpClient()

            Dim response As HttpResponseMessage = Await httpClient.PostAsync("http://redsquirrelsoftware.co.uk/trains/push/WPTileSubscribe.php", form)
            uri_timer.Stop()
        End If
    Catch ex As Exception
    End Try

End Sub
<?php
include 'WindowsPhoneTilePush.php';
$WPN = new WPN($packageSID, $clientSecret);
$xml_data = $WPN->build_tile_xml($sched_id, strtoupper($loc), "$eventTypeStr $dt (". strtolower($variation_status) . ")");
$WPNResponse = $WPN->post_tile($uri, $xml_data);
if($WPNResponse->error == true) {
    $my_file = $logFile;
    $handle = fopen($my_file, 'a') or die('Cannot open file:  '.$my_file);
    $data = $WPNResponse->httpCode . ":" . $WPNResponse->message . "\n";
    fwrite($handle, $data);
}
?>
这与预期的一样,URI存储在我的数据库中(从HTTPS开始,URI中也不包含throttledthirdparty,这表明此代码可以工作)

train\u movements.php:

Private Async Sub ApplicationBarPin_Click(sender As Object, e As EventArgs)
    ' Holds the push channel that is created or found.
            Dim pushChannel As HttpNotificationChannel

            ' The name of our push channel.
            Dim channelName As String = "WindowsPhoneTrainTileNotification"

            ' Try to find the push channel.
            pushChannel = HttpNotificationChannel.Find(channelName)

            ' If the channel was not found, then create a new connection to the push service.
            If pushChannel Is Nothing Then
                pushChannel = New HttpNotificationChannel(channelName, "redsquirrelsoftware.co.uk")
                uri_timer = New DispatcherTimer
                uri_timer.Interval = TimeSpan.FromSeconds(5)
                AddHandler uri_timer.Tick, AddressOf UriTimerTick
                uri_timer.Start()

                ' Register for all the events before attempting to open the channel.
                AddHandler pushChannel.ChannelUriUpdated, AddressOf PushChannel_TileChannelUriUpdated
                AddHandler pushChannel.ErrorOccurred, AddressOf PushChannel_TileErrorOccurred

                pushChannel.Open()

                pushChannel.BindToShellTile()
            Else
                ' The channel was already open, so just register for all the events.
                AddHandler pushChannel.ChannelUriUpdated, AddressOf PushChannel_TileChannelUriUpdated
                AddHandler pushChannel.ErrorOccurred, AddressOf PushChannel_TileErrorOccurred

                Dim form As New MultipartFormDataContent()
                form.Add(New StringContent(Statics.getUserID), "userId")
                form.Add(New StringContent(pushChannel.ChannelUri.ToString()), "uri")
                form.Add(New StringContent(Statics.CurrentScheduleId), "scheduleId")

                Dim httpClient As HttpClient = New HttpClient()

                Dim response As HttpResponseMessage = Await httpClient.PostAsync("http://redsquirrelsoftware.co.uk/trains/push/WPTileSubscribe.php", form)
            End If

            ShellTile.Create(New Uri("/Route.xaml?scheduleId=" & scheduleId, UriKind.Relative), secondaryTile, True) 'Create SecondaryTile and pass querystring to navigation url.
        Catch ex As Exception
        End Try

    End If
End Sub

Private Async Sub PushChannel_TileChannelUriUpdated(sender As Object, e As NotificationChannelUriEventArgs)
    Dim form As New MultipartFormDataContent()
    form.Add(New StringContent(Statics.getUserID), "userId")
    form.Add(New StringContent(e.ChannelUri.ToString()), "uri")
    form.Add(New StringContent(Statics.CurrentScheduleId), "scheduleId")

    Dim httpClient As HttpClient = New HttpClient()

    Dim response As HttpResponseMessage = Await httpClient.PostAsync("http://redsquirrelsoftware.co.uk/trains/push/WPTileSubscribe.php", form)
End Sub

Private Sub PushChannel_TileErrorOccurred(sender As Object, e As NotificationChannelErrorEventArgs)
    MessageBox.Show("Error creating live tile, please try again")
End Sub

Private Async Sub UriTimerTick(sender As Object, e As EventArgs)
    Try
        If pushChannel.ChannelUri IsNot Nothing Then
            Dim form As New MultipartFormDataContent()
            form.Add(New StringContent(Statics.getUserID), "userId")
            form.Add(New StringContent(pushChannel.ChannelUri.ToString()), "uri")
            form.Add(New StringContent(Statics.CurrentScheduleId), "scheduleId")

            Dim httpClient As HttpClient = New HttpClient()

            Dim response As HttpResponseMessage = Await httpClient.PostAsync("http://redsquirrelsoftware.co.uk/trains/push/WPTileSubscribe.php", form)
            uri_timer.Stop()
        End If
    Catch ex As Exception
    End Try

End Sub
<?php
include 'WindowsPhoneTilePush.php';
$WPN = new WPN($packageSID, $clientSecret);
$xml_data = $WPN->build_tile_xml($sched_id, strtoupper($loc), "$eventTypeStr $dt (". strtolower($variation_status) . ")");
$WPNResponse = $WPN->post_tile($uri, $xml_data);
if($WPNResponse->error == true) {
    $my_file = $logFile;
    $handle = fopen($my_file, 'a') or die('Cannot open file:  '.$my_file);
    $data = $WPNResponse->httpCode . ":" . $WPNResponse->message . "\n";
    fwrite($handle, $data);
}
?>
build_tile_xml($sched_id,strtoupper($loc),“$eventTypeStr$dt($strtolower($variation_status)。”);
$WPNResponse=$WPN->post_tile($uri,$xml_数据);
如果($WPNResponse->error==true){
$my_file=$logFile;
$handle=fopen($my_文件,'a')或die($my_文件:');
$data=$WPNResponse->httpCode.:“$WPNResponse->message.\n”;
fwrite($handle,$data);
}
?>
WindowsPhoneTilePush.php:(改编自)

access_令牌==''){
$this->get_access_token();
}
$headers=array('Content-Type:text/xml',“Content-Length:”.strlen($xml\u-data),“X-WNS-Type:$Type”,“授权:承载者$this->access\u令牌”);
如果($tileTag!=''){
数组推送($headers,“X-WNS-Tag:$tileTag”);
}
$ch=curl_init($uri);
#瓷砖:http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868263.aspx
# http://msdn.microsoft.com/en-us/library/windows/apps/hh465435.aspx
curl_setopt($ch,CURLOPT_CAPATH,“/var/www/clients/client1/web1/ssl/”);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_POSTFIELDS,“$xml_data”);
curl_setopt($ch,CURLOPT_VERBOSE,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl\u exec($ch);
$response=curl\u getinfo($ch);
卷曲关闭($ch);
$code=$response['http_-code'];
如果($code==200){
返回新的WPNResponse('Successfully sent message',$code);
}
else如果($code==401){
$my_file='/var/log/WPNotify.txt';
$handle=fopen($my_文件,'a')或die($my_文件:');
$data=日期(“d-M-y H:i:s:”)。“401授权错误:”。$this->访问\u令牌。“\n”;
fwrite($handle,$data);
如果($count==10){
出口
}
$this->access_token='';
返回$this->post_tile($uri,$xml_data,$type,$tileTag,$count++);
}
else if($code==410 | |$code==404){
返回新的WPNResponse('Expired or invalid URI',$code,true);
}
否则{
返回新的WPNResponse('发送消息时出现未知错误',$code,true);
}
}
}
?>
我的代码中可能有错误,也可能是我没有正确设置后端内容(大多数MSDN文档都是针对Windows服务器和.aspx的,因此我尝试复制这些指令,但可能在某个地方出错!)

如果有人能够帮助修复代码中的错误,或者为SSL证书提供客户端身份验证,我们将不胜感激

谢谢, 杰克

看看 无效的-token-when-trust-to-create-a-toast-n

请确保在请求访问令牌时,将应用程序的包SID作为“客户端id”参数而不是应用程序的客户端id发送


这确实令人困惑,两种方法都可以从Microsoft服务器获取访问令牌,但只有包SID请求的令牌在发送toast通知时不会给您401错误。

,这完全是自嘲,甚至可能与你期望我们费力阅读的大量代码无关。@RiggsFolly我知道401错误是什么,就在过去3天试图解决这个问题但没有结果之后,我希望这里的人能够对情况有所了解。我意识到那里有很多代码,但如果我问的问题没有任何代码,我保证第一条评论会是“发布你的代码”