2017-02-23 18 views
1

私は問題のabitを持っています、私はPHPのための通知ハブRESTラッパーを取得しようとしています。私は指示に従った、正しい資格情報を挿入し、まだ私はまだエラーメッセージが表示されます:Azure通知ハブ - 401 MissingToken:承認ヘッダーが見つかりませんでした

"Fatal error: Uncaught exception 'Exception' with message 'Error sending notificaiton: 401 msg: 401 MissingToken: The authorization header was not found..TrackingId:7392381b-7e4b-4ced-9090-6f3f8beac79d_G19,TimeStamp:2/23/2017 5:44:36 PM' in /"PATH"/class.NotificationHub.php:127 Stack trace: #0 /"PATH"/testingOfNotification.php(21): NotificationHub->sendNotification(Object(Notification), NULL) #1 {main} thrown in /"PATH"/class.NotificationHub.php on line 127"

私は続いガイドがthis guideです。ここで

が私のコードです:

require_once("class.NotificationHub.php"); 

    $hub = new NotificationHub("Endpoint=sb://XXXX.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=XXXX", "XXXX"); 

$message = '{"data":{"message":"Hello from PHP!"}}'; 

$notification = new Notification("gcm", $message); 

$hub->sendNotification($notification, null); 

class.Notification.php

class Notification { 
    public $format; 
    public $payload; 
    public $headers; 

    function __construct($format, $payload) { 
     if (!in_array($format, ["template", "apple", "windows", "gcm", "windowsphone"])) { 
      throw new Exception('Invalid format: ' . $format); 
     } 
     $this->format = $format; 
     $this->payload = $payload; 
    } 
} 

class.NotificationHub

include 'class.Notification.php'; 

class NotificationHub { 

const API_VERSION = "?api-version=2013-10"; 
private $endpoint; 
private $hubPath; 
private $sasKeyName; 
private $sasKeyValue; 

function __construct($connectionString, $hubPath) { 

    $this->hubPath = $hubPath; 
    $this->parseConnectionString($connectionString); 
} 

private function parseConnectionString($connectionString) { 

    $parts = explode(";", $connectionString); 

    if (sizeof($parts) != 3) { 
     throw new Exception("Error parsing connection string: " . $connectionString); 
    } 

    foreach ($parts as $part) { 
     if (strpos($part, "Endpoint") === 0) { 
      $this->endpoint = "https" . substr($part, 11); 
     } else if (strpos($part, "SharedAccessKeyName") === 0) { 
      $this->sasKeyName = substr($part, 20); 
     } else if (strpos($part, "SharedAccessKey") === 0) { 
      $this->sasKeyValue = substr($part, 16); 
     } 
    } 
} 

public function generateSasToken($uri) { 

    $targetUri = strtolower(rawurlencode(strtolower($uri))); 
    $expires = time(); 
    $expiresInMins = 60; 
    $expires = $expires + $expiresInMins * 60; 
    $toSign = $targetUri . "\n" . $expires; 
    $signature = rawurlencode(base64_encode(hash_hmac('sha256', $toSign, $this->sasKeyValue, TRUE))); 
    $token = "SharedAccessSignature sr=" . $targetUri . "&sig=" 
     . $signature . "&se=" . $expires . "&skn=" . $this->sasKeyName; 

    return $token; 
} 

public function broadcastNotification($notification) { 
    $this->sendNotification($notification, ""); 
} 

public function sendNotification($notification, $tagsOrTagExpression) { 

    echo $tagsOrTagExpression."<p>"; 

    if (is_array($tagsOrTagExpression)) { 
     $tagExpression = implode(" || ", $tagsOrTagExpression); 
    } else { 
     $tagExpression = $tagsOrTagExpression; 
    } 

    # build uri 
    $uri = $this->endpoint . $this->hubPath . "/messages" . NotificationHub::API_VERSION; 
    echo $uri."<p>"; 

    $ch = curl_init($uri); 

    if (in_array($notification->format, ["template", "apple", "gcm"])) { 
     $contentType = "application/json"; //;charset=utf-8 
    } else { 
     $contentType = "application/xml"; 
    } 
    $token = $this->generateSasToken($uri); 

    //printArr($token, "token"); die(); 

    $headers = [ 
     'Authorization: '.$token, 
     'Content-Type: '.$contentType, 
     'ServiceBusNotification-Format: '.$notification->format 
    ]; 

    printArr($headers); 

    if ("" !== $tagExpression) { 
     $headers[] = 'ServiceBusNotification-Tags: '.$tagExpression; 
    } 

    # add headers for other platforms 
    if (is_array($notification->headers)) { 
     $headers = array_merge($headers, $notification->headers); 
    } 

    curl_setopt_array($ch, array(
     //CURLOPT_CUSTOMREQUEST => 'PUT', 
     CURLOPT_POST => TRUE, 
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_SSL_VERIFYPEER => FALSE, 
     CURLOPT_HTTPHEADER => $headers, 
     CURLOPT_POSTFIELDS => $notification->payload 
    )); 
    // Send the request 
    $response = curl_exec($ch); 

    // Check for errors 
    if($response === FALSE){ 
     throw new Exception(curl_error($ch)); 
    } 

    $info = curl_getinfo($ch); 
    if ($info['http_code'] <> 201) { 
     throw new Exception('Error sending notificaiton: '. $info['http_code'] . ' msg: ' . $response); 
    } 
    //print_r($info); 
    //echo $response; 
} 
} 

だから私の質問だった私が間違っているつもりですか?私は、Azureから送信されたテスト通知が自分のデバイスに表示されるため、Azure Notification HubからAndroidアプリへの接続が機能していることを知っています。 The authorization header was not found

答えて

0

エラーがあることを示しています。しかし、ヘッダはすでにあなたのコードに渡されています。だから、投稿したコードがあなたのサーバー上で実行されたものとまったく同じかどうかを再度確認してください。

私も自分のサイトでそれをテストし、うまくいきました。ここで

は私のスクリーンショットです:

enter image description here

enter image description here

+0

は、返信いただきありがとうございます:) 昨日の私はAzureの新しい名前空間と通知ハブを作っ質問を投稿する前に、私はそれを試してみました同じ結果を得ました。 しかし、今はどういうわけか、私は実際には何の手がかりも持っていません。 ありがとうございます。 - クリスチャン – Tenderiser

関連する問題