firebase / quickstart-java

Quickstart samples for Firebase Java Admin SDK
Apache License 2.0
234 stars 143 forks source link

Getting NullPointerException for AccessToken #38

Closed BaljitKarwal closed 9 months ago

BaljitKarwal commented 3 years ago

I am new to firebase however I did setup service account in console and used "messaging.java". Downloaded proper json file and updated all required variables in file.

I am getting this error now for getting access token from GoogleCredentials. I am unable to figure out what am I missing. Is it any variable misisng in my credentials file? or scope?

java.lang.NullPointerException at Messaging.getAccessToken

Can someone please advise? Below is my code:

` private static final String PROJECT_ID = ""; private static final String BASE_URL = "https://fcm.googleapis.com"; private static final String FCM_SEND_ENDPOINT = "/v1/projects/" + PROJECT_ID + "/messages:send";

private static final String MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging"; private static final String[] SCOPES = { MESSAGING_SCOPE };

private static final String TITLE = "FCM Notification"; private static final String BODY = "Notification from FCM"; public static final String MESSAGE_KEY = "message"; public static final String FIREBASE_SERVICE_PATH = "firebase_service_account/google-services.json";

/**

Regards Baljit

BaljitKarwal commented 3 years ago

I found solution for this, using below code of line before calling AccessToken(); googleCredentials.refreshIfExpired(); and removed googleCredentials.refreshAccessToken();

huSSooxXx commented 3 years ago

I found solution for this, using below code of line before calling AccessToken(); googleCredentials.refreshIfExpired(); and removed googleCredentials.refreshAccessToken();

I just changed below and its worked

        googleCredentials.refreshIfExpired();
        //googleCredentials.refreshAccessToken();

Thank you @BaljitKarwal

SEOJAEHWA commented 2 years ago

I found solution for this, using below code of line before calling AccessToken(); googleCredentials.refreshIfExpired(); and removed googleCredentials.refreshAccessToken();

It works. thanks.

dineshsraman commented 9 months ago

I'm also using similar code, but its throwing error. Any help?

Code: private static String getAccessToken() throws IOException { String[] SCOPES = {"https://www.googleapis.com/auth/firebase.messaging"};

    GoogleCredentials googleCredentials = GoogleCredentials.fromStream(new FileInputStream("./src/service-account.json")).createScoped(Arrays.asList(SCOPES));
    //googleCredentials.refresh();
    try {
        googleCredentials.getAuthenticationType();
        googleCredentials.refreshIfExpired();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return googleCredentials.getAccessToken().getTokenValue();

    }

Error from refreshIfExpired call: Error getting access token for service account: Remote host terminated the handshake, iss: firebase-adminsdk-jnf15@*****

BaljitKarwal commented 9 months ago

I'm also using similar code, but its throwing error. Any help?

Code: private static String getAccessToken() throws IOException { String[] SCOPES = {"https://www.googleapis.com/auth/firebase.messaging"};

    GoogleCredentials googleCredentials = GoogleCredentials.fromStream(new FileInputStream("./src/service-account.json")).createScoped(Arrays.asList(SCOPES));
    //googleCredentials.refresh();
    try {
      googleCredentials.getAuthenticationType();
      googleCredentials.refreshIfExpired();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return googleCredentials.getAccessToken().getTokenValue();

  }

Error from refreshIfExpired call: Error getting access token for service account: Remote host terminated the handshake, iss: firebase-adminsdk-jnf15@*****

I would suggest checking your JAVA_HOME and TLS versions.

thuannd97 commented 9 months ago

I found solution for this, using below code of line before calling AccessToken(); googleCredentials.refreshIfExpired(); and removed googleCredentials.refreshAccessToken();

It's worked for me. Thank u