Connect android backend endpoints and client using oauth2
I have a problem where I intend to authorize Android app with @ApiMethod on endpoint. I read a lot of documentation but I didn't solve the problem.
The main error I see in the logs is GoogleAuthException: Unknown.
I created a project in the Google Developers Console with two clients - one for Android and one for audience / web_id.
My client code in android:
mCredential = GoogleAccountCredential.usingAudience(this,"server:client_id:"+Constants.WEB_CLIENT_ID);
chooseAccount();
In onActivityResult get the account name:
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_ACCOUNT_PICKER:
if (data != null && data.getExtras() != null) {
String accountName =
data.getExtras().getString(
AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
mCredential.setSelectedAccountName(accountName);
}
}
break;
}
Then I create an AsyncTask for the endpoint method of the call and add the credentials:
BpmApiRegister.Builder builder = new BpmApiRegister.Builder(AndroidHttp.newCompatibleTransport(),new JacksonFactory(),mCredential)
.setRootUrl(Constants.CLOUD_URL)//https://project_id.appspot.com/_ah/api/
.setApplicationName("BPM");
User user = createUser();
BpmApiRegister registerapi = builder.build();
try {
return registerapi.create(user).execute();//PROBLEM HERE
} catch (Exception e) {
e.printStackTrace();
}
Endpoint code:
@Api(name = "bpmApiRegister",version = "v1",resource = "users", namespace = @ApiNamespace(
ownerDomain = "backend.myapplication.example.com",
ownerName = "backend.myapplication.example.com",
packagePath = "" ))
public class UserRegisterEndpoint {
private static final Logger logger = Logger.getLogger(UserRegisterEndpoint.class.getName());
@ApiMethod(name = "create",path="users",httpMethod = ApiMethod.HttpMethod.POST,
scopes = {BackendConstants.EMAIL_SCOPE},
clientIds = {
BackendConstants.WEB_CLIENT_ID,
BackendConstants.ANDROID_CLIENT_ID,
Constant.API_EXPLORER_CLIENT_ID},
audiences = {BackendConstants.ANDROID_AUDIENCE})
public User create(User user, com.google.appengine.api.users.User userAuth) throws OAuthRequestException {
Error in execution endpoint method:
04-25 11:00:11.655 19784-19927/com.android.app W/System.err﹕ com.google.android.gms.auth.GoogleAuthException: Unknown 04-25 11:00:11.655 19784-19927/com.android.app W/System.err﹕ at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 04-25 11:00:11.655 19784-19927/com.android.app W/System.err﹕ at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
To create android client_id I got a fingerprint and I put it in com.android.app package (my app package).
To create a web_id, I put two urls http: // localhost: 8080 and https://project_id.appspot.com ; and our corresponding redirect url add / oauth2callback
source to share
No one has answered this question yet
Check out similar questions: