Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to get Scopes dependencies for java for new Firebase Cloud Message API?

FCM Api was updated to v1, and now u can't pass key=<API_KEY> (once generated from FCM Console) header as before, now u should generate it by SDK com.google.api-client and there u should pass mystery SCOPES inside createScoped() method. Here is a lot of examples about it - without information about scopes. But what is this? where to get it? I can't to find any information about it. Please help me

like image 886
qwert_ukg Avatar asked Dec 08 '25 08:12

qwert_ukg


2 Answers

The scope is list of String. I have tried several endpoints from https://developers.google.com/identity/protocols/googlescopes and worked.

  1. "https://www.googleapis.com/auth/firebase"
  2. "https://www.googleapis.com/auth/cloud-platform"
  3. "https://www.googleapis.com/auth/firebase.readonly"

    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
    
like image 83
adrianekafikri Avatar answered Dec 09 '25 21:12

adrianekafikri


This way of getting access token is not working I tried.But u can try a direct http request.

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);
    }
}

}

like image 43
abinash sahoo Avatar answered Dec 09 '25 20:12

abinash sahoo