Skip to content

Getting NullPointerException for AccessToken #38

@BaljitKarwal

Description

@BaljitKarwal

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";

/**

  • Retrieve a valid access token that can be use to authorize requests to the FCM REST
  • API.
  • @return Access token.
  • @throws IOException
    */
    private static String getAccessToken() throws IOException {
    GoogleCredentials googleCredentials = GoogleCredentials
    .fromStream(new FileInputStream(FIREBASE_SERVICE_PATH));
    try {
    googleCredentials.getAuthenticationType();
    googleCredentials.refreshAccessToken();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return googleCredentials.getAccessToken().getTokenValue();
    }`

Regards
Baljit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions