How to get an oauth2 authentication token from google cloud (in codename one) and then use it to make a request to the cloud natural language API

Viewed 120

I am trying to build an app in codename one with the cloud natural language api but I am struggling to figure out how to first get authentication from oauth2 and then make a request to the api.

I have read the quickstart for the cloud natural language api and followed all of the steps. I can make requests to the api from the gcloud command line but I want to be able to make requests from codename one. I want to use oauth2 to get authentication and have an oauth2 client ID but I do not know how to get the authentication token. I have read about it here https://www.codenameone.com/google-login.html but I do not know what to put HERE on line 5 of the following code. Once I have the oauth2 authentication token, I do not know how I will make a request to the api from codename one. I have read about requests to the natural language api here https://cloud.google.com/natural-language/docs/reference/libraries but it did not say how to make a request from codename one.

    Login gc = GoogleConnect.getInstance();
    gc.setClientId(clientId);
    gc.setRedirectURI(redirectURI);
    gc.setClientSecret(clientSecret);
    gc.setCallback(**HERE**);
    if(!gc.isUserLoggedIn()){
        gc.doLogin();
    }else{
        //get the token and now you can query the gplus API
        String token = gc.getAccessToken().getToken();
    }

In summary, I do not know how to get an oauth2 token from codename one and then make a request to the cloud natural language api with that token.

1 Answers

You should use a slightly newer version of this article here under the "Google Sign-In" section. The token is returned in this line of code String token = gc.getAccessToken().getToken(); but it will only work within the callback code when login was successful.

You will need to use the REST API to make the requests to the server as covered here: https://cloud.google.com/natural-language/docs/reference/rest/

That way it will work for all supported platforms.

Related