从哪里获取新的Firebase Cloud Message API的Java的Scopes依赖项?

时间:2017-11-16 08:46:13

标签: java api push firebase-cloud-messaging

FCM Api已更新为v1,现在您无法像以前一样通过key=<API_KEY>(从FCM控制台生成)标头,现在您应该通过SDK com.google.api-client生成它应该在createScoped()方法中传递神秘的SCOPES。 Here有很多关于它的例子 - 没有关于范围的信息。但这是什么?在哪里得到它?我无法找到有关它的任何信息。请帮帮我

3 个答案:

答案 0 :(得分:2)

范围是String列表。我已经尝试了https://developers.google.com/identity/protocols/googlescopes的几个端点并且工作了。

  1. &#34; https://www.googleapis.com/auth/firebase&#34;
  2. &#34; https://www.googleapis.com/auth/cloud-platform&#34;
  3. &#34; https://www.googleapis.com/auth/firebase.readonly&#34;

    val googleCredential = GoogleCredential.fromStream("yourjson.json")            
    .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase", 
    "https://www.googleapis.com/auth/cloud-platform", 
    "https://www.googleapis.com/auth/firebase.readonly"))
    googleCredential.refreshToken()
    return googleCredential.accessToken
    

答案 1 :(得分:1)

这种获取访问令牌的方法无效我试过。但是你可以尝试直接的http请求。

public static void main(String args[]) throws IOException {
       public final static String   AUTH_KEY_FCM    = "server key";
       public final static String   API_URL_FCM     = 
                                      "https://fcm.googleapis.com/fcm/send";

    // Method to send Notifications from server to client end.

    // userDeviceIdKey is the device id you will query from your database

    String authKey = AUTH_KEY_FCM; // You FCM AUTH key
    String FMCurl = API_URL_FCM;

    URL url = new URL(FMCurl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "key=" + authKey);
    conn.setRequestProperty("Content-Type", "application/json");

    JSONObject json = new JSONObject();
    json.put("to",
            "Device key");
    JSONObject info = new JSONObject();
    info.put("title", "Demo"); // Notification title
    info.put("body", "Hello Test notification"); // Notification body
    json.put("notification", info);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(json.toString());
    wr.flush();
    conn.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }
}

}

答案 2 :(得分:0)

授权范围是我在页面上找到的列表。起初我无法理解它们的用途,但后来我猜到了

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send