connect endpoints-backend and android client with oauth2

时间:2015-04-25 11:14:47

标签: java android oauth-2.0 google-oauth google-cloud-endpoints

I have a problem when I intent authorized android app with @ApiMethod in endpoint. I read a lot of documentation, but I didn't resolved the problem.

The principal error that I see in logs is GoogleAuthException: Unknown.

I have created a project in Google Developers Console, moreover two clients_id 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 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 AsyncTask for call endpoint method and add 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(); 
        }

The 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 {

The error in execute 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 have obtained the fingerprint and I put it package com.android.app(my app package).

To create web_id, I put two url's http://localhost:8080https://project_id.appspot.com调用c#函数;和我们各自的重定向网址添加/ oauth2callback

0 个答案:

没有答案