1

为了在之前使用 Google API 需要在 Google 帐户中进行身份验证,并使用 OAuth2 获取身份验证令牌。好吧,我会知道是否有可能做相反的事情:在 Android 中使用 OAuth2,因为我在不使用 Google API 的情况下在 Google 帐户中进行身份验证。我只需要在 Google 帐户中进行身份验证即可执行 Google Apps 脚本脚本,即仅用于向这些脚本发出 GET 或 POST 参数化请求,这些脚本将根据其代码处理此参数,仅此而已。

4

1 回答 1

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);
        [..]
    }

我希望它可以帮助某人。不管怎么说,还是要谢谢你。

于 2013-02-19T17:36:06.640 回答