簡體   English   中英

使用php向Google Cloud Messaging發送通知給我未經授權的錯誤401

[英]Sending notifications to Google Cloud Messaging with php gives me Unauthorized Error 401

搜索有關如何使用GCM但使用PHP而不是servlet來發送通知的一些信息,我發現了這一點: 使用PHP的GCM(Google雲消息傳遞)

我測試了這些問題的響應的工作代碼,還為瀏覽器應用程序(帶有引薦來源)創建了一個密鑰,並授予了該ip的權限: .mywebsite.com / (此url上有php文件:“ http: //www.mywebsite.com/~jma/cHtml5/cap/kk.php “)

但我收到此響應: 未經授權的錯誤401

我做錯了什么?

這是PHP文件:

<?php
    // Replace with real server API key from Google APIs        
    $apiKey = "fictional key";

    // Replace with real client registration IDs
    $registrationIDs = array( "APA91asdasdSDGGS232S13S4213abGqiNhCIXKjlxrkUYe_xTgTacNGB5n16b380XDd8i_9HpKGRHkvm8DDet4_WK3zumjDEKkTRWLgPS7kO-BrKzWz7eWFQaDD9PJ8zA6hlSqL9_zH21P8K22ktGKmo_VIF6YAdU9ejJovrKBTpgQktYkBZBf9Zw","APAasdasd32423dADFG91bHYYxYB7bFiX5ltbJt6A-4MBiNg7l4RS4Bqf3jIfYviaaUfZ810XJo2o66DY9-jdeJk_JR8FIZCyrmCv-eu_WLkGZ8KaoHgEDR_16H2QPm98uHpe1MjKVXbzYc4J89WMmcIrl5tHhWQnIQNzaI6Zp6yyFUNUQ");

    // Message to be sent
    $message = "Test Notificación PHP";

    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registrationIDs,
        'data' => array( "message" => $message ),
    );

    $headers = array(
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
    );

    // Open connection
    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    //curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($ch, CURLOPT_POST, true);
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));

    // Execute post
    $result = curl_exec($ch);

    // Close connection
    curl_close($ch);
    echo $result;
    //print_r($result);
    //var_dump($result);
?>

解決了!!!

您必須對服務器應用(使用IP鎖定)使用密鑰,而不要使用瀏覽器密鑰

:)

您需要在服務器端編碼的API密鑰字段中為服務器應用程序使用密鑰。 創建服務器密鑰時,請勿在IP地址字段內輸入任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM