Android:在没有Google API的Google帐户中使用OAuth2进行身份验证

时间:2013-02-15 18:22:35

标签: android google-api oauth-2.0

要使用Google API,必须先在Google帐户中进行身份验证,然后使用OAuth2获取身份验证令牌。 好吧,我知道是否有可能做相反的事情:在Android中使用OAuth2,我在Google帐户中进行身份验证而不使用Google API。 我只需要在Google帐户中进行身份验证即可执行Google Apps脚本脚本,即仅用于对这些脚本进行GET或POST参数化请求,这些脚本将根据其代码处理此参数,仅此而已。

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。 代码:

public class UploadFileGASActivity extends Activity {
    public final static String GAS_HELLO_WORLD = "url_to_gas_script";
    public static boolean authTokenInvalidated = false;

    public class OnTokenAdquired implements AccountManagerCallback<Bundle>{
        public void run(AccountManagerFuture<Bundle> future){
            Log.v("CALLBACK","post-getAuthToken");
            try {
                Bundle authTokenBundle = future.getResult();

                if ( authTokenBundle.containsKey(AccountManager.KEY_AUTHTOKEN)){
                    String authTokenStr = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
                    String accountName = authTokenBundle.getString(AccountManager.KEY_ACCOUNT_NAME);
                    String accountType = authTokenBundle.getString(AccountManager.KEY_ACCOUNT_TYPE);
                    Log.v("ACCOUNT NAME",accountName);
                    Log.v("ACCOUNT TYPE",accountType);
                    Log.v("TOKEN",authTokenStr);
                    if (!authTokenInvalidated) {
                        invalidateAuthToken("com.google",authTokenStr);
                        authTokenInvalidated = true;
                        Log.v("CALLBACK","authTokenInvalidated == true");
                        getAuthToken();
                        return;
                    }
                    else {
                        authTokenInvalidated = false;
                        Log.v("CALLBACK","authTokenInvalidated == false");
                    }
                    HttpUtil httpUtil = new HttpUtil();
                    String responseStr = httpUtil.getResponseFromHttpRequest(GAS_HELLO_WORLD, authTokenStr);
                    Log.v("GAS_HELLO_WORLD",responseStr);
                    TextView textView = (TextView)findViewById(R.id.text_upload_file_gas);
                    textView.setText(authTokenStr);
                }
                else {
                    Log.v("KEY_AUTHTOKEN","non hai auth token, normal cando non hai conexion");
                }
                if (authTokenBundle.containsKey(AccountManager.KEY_INTENT)) {
                    Log.e("KEY_INTENT","temos KEY_INTENT");
                    Intent launch = (Intent)authTokenBundle.get(AccountManager.KEY_INTENT);
                    startActivityForResult(launch,0);
                    return;
                }
            }
            catch (AuthenticatorException e){
                Log.e("OnTokenAdquired.run","AuthenticatorException");
            }
            catch (IOException e){
                Log.e("OnTokenAdquired.run","IOException");
            }
            catch (OperationCanceledException e){
                Log.e("OnTokenAdquired.run","OperationCanceledException");
            }
        }
    }
    public void getAuthToken() {
        AccountManager accountManager = AccountManager.get(this);
        Bundle options = new Bundle();
        OnTokenAdquired onTokenAdquired = new OnTokenAdquired();
        Handler handler = new Handler();
        Account[] accounts = accountManager.getAccountsByType("com.google");
        String authTokenType = "oauth2:https://www.googleapis.com/auth/drive";
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accounts[1], 
            authTokenType, options, this, onTokenAdquired, handler);
    }
    public void invalidateAuthToken(String accountType, String authToken){
        AccountManager accountManager = AccountManager.get(this);
        accountManager.invalidateAuthToken(accountType, authToken);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //Activity.RESULT_OK;
        Bundle extras = data.getExtras();
        Set<String> keys = extras.keySet();
        Iterator<String> iter = keys.iterator();
        Log.v("KEY_INTENT","requestCode: "+requestCode+", resultCode: "+resultCode);
        Log.v("KEY_INTENT","EXTRAS KEY_INTENT");
        while (iter.hasNext()){
                Log.v("KEY_INTENT",(String)iter.next());
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_file_gas);
            Log.v("GAS","UploadFileGASActivity called.");
        getAuthToken();
    }
}

而且,在HttpUtil中:

    public String getResponseFromHttpRequest(String httpRequest, String authToken) {
        [..]
        request.setHeader("Authorization", "OAuth "+authToken);
        [..]
    }

我希望它有助于某人。 不管怎样,谢谢。