Nicholas Pesa
Nicholas Pesa

Reputation: 2196

cloud endpoints return error 401 unauthorized request

So i have made a few cloud endpoints to be used in an application and am going through the process of securing them in an android client. Everytime i try and consume data in the android client code, i get error 401 "Unautorized Access". I can tell why the exception is being thrown as i am nullchecking the 'User' parameter on every api method. But my question is why i am always getting a null User for my api methods. I have successfully given my endpoint builder the correct credentials and setSelectedAccountName with the account on the phone. here is my example:

One api method:

 public CollectionResponse<Student> getAllStudents(User user) throws OAuthRequestException {
        if (user == null)
               throw new OAuthRequestException("BAD_USER");
        else {
               List<Student> stud = ofy().load().type(Student.class).list();
               return CollectionResponse.<Student>builder().setItems(stud).build();
           }
    }

this is placed at the top of the api as well:

@Api(name = "studentEndpoint", version = "v1", namespace = @ApiNamespace(ownerDomain = "appbackend.mymodule.example.com", ownerName = "appbackend.mymodule.example.com", packagePath=""),
            scopes = {Constants.EMAIL},
            clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID},
            audiences = {Constants.ANDROID_AUDIENCE})

I am grabbing the account email using account manager in my android client, setting up credentials and getting an access token.

GoogleAccountCredential cred = GoogleAccountCredential.usingAudience(AddUser.this, AppConstants.AUDIENCE);
                cred.setSelectedAccountName(emailAccount);

this is how i am using it in my client code:

StudentEndpoint service = AppConstants.getApiHandle(cred);
                stud = service.getAllStudents().execute();

The above code is being called asynchronously.

I have triple checked my client ids to make sure they are correct and they are. I am also installing this from android studio directly onto my phone via adb. I have my debug SHA1 in the the google cloud console, but do i need my release SHA1 there as well. I didnt think i would because i have not created release keys for this app yet and have only used an unsigned copy. As well if i use the web-client I can authenticate but everytime i make a request i get error 401, and i see my exception 'BAD_USER' thrown. I do have a 'Bearer' key tho showing that i have access.

If i cancel the nullcheck on the User parameter in my module the methods work fine and i can get the data from my datastore. So any reason why i am getting a null user everytime i make api requests? Any help is much appreciated. Thanks in advance.

Upvotes: 1

Views: 1179

Answers (1)

Romin
Romin

Reputation: 8806

I would suggest to revisit the following 3 points:

  • Make sure that your package name is correct i.e. same in both the Android app and the package name that was specified when creating the credentials in the Cloud Console.

  • Can you check the value of the AppConstants.AUDIENCE in your code. Ideally it needs to be in the following format "server:client_id:WEB_CLIENT_ID'

  • Ensure that your Constants.WEB_CLIENT_ID is the same as Constants.ANDROID_AUDIENCE

Upvotes: 1

Related Questions