1

我想在我的应用程序中使用 google 身份验证登录。为此,我已按照此链接 链接链接

我的代码:

package com.android.mytokengenerator;

import java.io.IOException;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.Scopes;

import android.os.AsyncTask;
import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
    String scope = "audience:server:client_id:SERVER CLIENT KEY";
    int MY_ACTIVITYS_AUTH_REQUEST_CODE = 200;@
    Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new getToken().execute(null, null, null);

    }

    @
    Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_ACTIVITYS_AUTH_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                new getToken().execute(null, null, null);
            }
        }
    }
    public class getToken extends AsyncTask < Void, Void, Void > {

        @
        Override
        protected Void doInBackground(Void...params) {
            // TODO Auto-generated method stub
            getAndUseAuthTokenBlocking();

            return null;
        }

    }
    // Example of how to use the GoogleAuthUtil in a blocking, non-main thread context
    void getAndUseAuthTokenBlocking() {

        // Retrieve a token for the given account and scope. It will always return either
        // a non-empty String or throw an exception.
        String[] account = getAccountNames();
        for (int i = 0; i < account.length; i++) {
            try {
                final String token = GoogleAuthUtil.getToken(MainActivity.this, account[i], "oauth2:" + Scopes.PLUS_PROFILE);
                Log.e("token", token);
                // Do work with token.
                //                        if (server indicates token is invalid) {
                //                            // invalidate the token that we found is bad so that GoogleAuthUtil won't
                //                            // return it next time (it may have cached it)
                //                            GoogleAuthUtil.invalidateToken(Context, String)(context, token);
                //                            // consider retrying getAndUseTokenBlocking() once more
                //                            return;
                //                        }
                return;
            } catch (GooglePlayServicesAvailabilityException playEx) {
                Dialog alert = GooglePlayServicesUtil.getErrorDialog(
                    playEx.getConnectionStatusCode(),
                    this,
                    MY_ACTIVITYS_AUTH_REQUEST_CODE);

            } catch (UserRecoverableAuthException userAuthEx) {
                Log.e("UserRecoverableAuthException", "UserRecoverableAuthException");
                userAuthEx.printStackTrace();
                // Start the user recoverable action using the intent returned by
                // getIntent()
                MainActivity.this.startActivityForResult(
                    userAuthEx.getIntent(),
                    MY_ACTIVITYS_AUTH_REQUEST_CODE);
                return;
            } catch (IOException transientEx) {
                Log.e("IOException", "IOException");
                transientEx.printStackTrace();

                // network or server error, the call is expected to succeed if you try again later.
                // Don't attempt to call again immediately - the request is likely to
                // fail, you'll hit quotas or back-off.
                return;
            } catch (GoogleAuthException authEx) {
                Log.e("GoogleAuthException", "GoogleAuthexception");
                authEx.printStackTrace();

                // Failure. The call is not expected to ever succeed so it should not be
                // retried.
                return;
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }


    }
    private String[] getAccountNames() {
        AccountManager mAccountManager = AccountManager.get(this);
        Account[] accounts = mAccountManager.getAccountsByType(
            GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
        String[] names = new String[accounts.length];
        for (int i = 0; i < names.length; i++) {
            names[i] = accounts[i].name;
        }
        return names;
    }
}

现在,当我输入范围值
String scope = "audience:server:client_id:SERVER CLIENT KEY";
给出异常 UnknownResource

当为范围 String scope = Scopes.PLUS_PROFILE 赋值时;给予例外

日志猫

没有得到什么问题。在通过GoogleAuthUtil.getToken()生成令牌之前我需要使用PlusClient Connectivity。请帮助

4

0 回答 0