0

I am developing an android app for an online Newspaper company. The company already developed and hosted their APIs on the Google App Engine with OAuth 2.0 support.
So I am to develop an android app that communicates with their deployed backend API with OAuth 2.0 support and fetch the contents Response from their assigned Google API Explorer.

so according to the Google cloud endpoints documentation for android clients(like my app) trying to make authenticated calls to the endpoints with OAuth 2.0 support, I was directed to:

  1. Configure my Android Client(my android app) to provide credentials to the service object
  2. se the account picker to support user choice of login accounts.

I followed the instructions on the Google Cloud Endpoint website but I didn't understand the sample codes that was used to explain, so I tried coming up with this:

class EndpointsAsyncTask extends AsyncTask<Void, Void, Void>{

       @Override
       protected Void doInBackground(Void... params) {

           HttpTransport httpTransport = new NetHttpTransport();
           JsonFactory jsonFactory = new  AndroidJsonFactory();


           try {
               GoogleCredential credential = new GoogleCredential.Builder()
                       .setTransport(httpTransport)
                       .setJsonFactory(jsonFactory)
                       .setServiceAccountId(CLIENT_EMAIL)
                       .setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/userinfo.email"))
                       .setServiceAccountPrivateKeyFromP12File(new File("file.p12"))
                       .build();

           /*so now what do I do with the credential object 
          and how do I set the root URL (https://project-id-endpoints.appspot.com/_ah/api)
          that the android client(this android app)
         will connect to in the backend API call
         */

           }
           catch(GeneralSecurityException gse){
               gse.printStackTrace();
           }
           catch(IOException ioe){

           }


           return null;
       }

       @Override
       protected void onPostExecute(Void aVoid) {
           super.onPostExecute(aVoid);

       }
   }

whenever I run my codes, the log report on the Google Developer Console is saying

"Uauthorised access" meaning the authentication call is not working...

Below is my code for opening a GET URL connection to one of the API service content:

public String open() throws IOException{

        InputStream inputStream = null;
        int len = 500;

        try {
            URL url = new URL("https://project-name-api-endpoints.appspot.com/_ah/api/core/v5.1.1/info/category/en");

            URLConnection urlConnection = url.openConnection();

            inputStream = urlConnection.getInputStream();

            String content = readIt(inputStream, len);

            return content;
        }//end try

        finally {
            if(inputStream != null)
            {
                inputStream.close();
            }
        }

    }

The question I have is:

What do I do with the credential object and how do I set the root URL (https://project-id-endpoints.appspot.com/_ah/api) that the android client(this android app) will connect to in the backend API call

rene
  • 41,474
  • 78
  • 114
  • 152
Isaac
  • 296
  • 1
  • 4
  • 14
  • my problem is that.. I don't understand the google documentation I shared the link for... and I need help connecting my android app to the google api endpoints – Isaac Mar 23 '15 at 19:03
  • It sounds like you want to take your existing App Engine project and turn it into a Cloud Endpoint that an Android App connects to, is that right? – 1.618 Mar 23 '15 at 19:07
  • "*I don't understand the google documentation I shared the link for*" We can't really explain how stuff works on a Q&A site. You need to keep reading until you understand it, ask in a forum (where discussions are more welcomed) or post a more clear cut question here about a specific part of the code you are struggling with. – James Mar 23 '15 at 19:40
  • I have restructured my questions all over hoping for a better understanding and answer.. thank you... @rene – Isaac Mar 24 '15 at 10:01
  • @LynnCrumbling i already restructured my question all over again... please check through again.. thank you – Isaac Mar 24 '15 at 10:04
  • @Isaac Nice! You'll have a much better shot at getting an answer now... +1 – Lynn Crumbling Mar 24 '15 at 12:52
  • @1.618 the app engine project is already a cloud endpoint.. so yes I want to my android app to connect to it... – Isaac Mar 26 '15 at 07:11

1 Answers1

1

The problem is that you are not using those credentials you set up when calling the endpoint. That connection you are making is completely unaware of all the OAuth settings and wonderful stuff you get from endpoints + Android.

Ideally what you need to do is:

1) Create the client libraries for Android from your endpoints (As explained here). Do not make "manual" (through URLConenction) calls to your endpoints, although it's technically possible it's absolutely not recommended.

2) With those libs (jars) included in your project all you need to do is call whatever method you need (as explained here) and the libraries will include all required authorization headers, etc based on your app settings. You don't need to worry about anything else regarding OAuth.

TIP: Make sure to include all of your developer's SHA debug signatures on the authorization on Google console. If you don't, you'll only be able to call the endpoints from your "prodcution" app.

jirungaray
  • 1,674
  • 1
  • 11
  • 18
  • okay, so the client lib has been generated. But according to this [link](https://cloud.google.com/appengine/docs/java/endpoints/consume_android), do I still need to add the template endpoint module to my project or how do I add this client libraries to my project.. because my client libraries are in .zip format (the Backend API was developed using Python) – Isaac Mar 27 '15 at 06:02
  • I've always used Java for the backend (and get a jar File straight up), but a quick research shows you should unzip that file and import the contained lib into Android Studio. – jirungaray Mar 27 '15 at 13:12
  • there is no lib in the unzipped folder... please refer to this [link](http://stackoverflow.com/questions/29307553/how-to-build-api-client-library-using-gradle-for-my-android-app) for a detailed question about my issue.. – Isaac Mar 27 '15 at 18:21