为了在之前使用Google API,必须在Google帐户中进行身份验证,并使用OAuth2获取身份验证令牌。好吧,我知道是否可以做相反的事情:在Android中使用OAuth2,因为我在谷歌帐户中进行身份验证,而不使用谷歌API。我只需要在谷歌账户中进行身份验证就可以执行谷歌应用程序脚本,也就是说,只需要对这些脚本进行GET或POST参数化请求,这些脚本将根据其代码处理这些参数,仅此而已。
是的,这是可能的。代码:
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);
[..]
}
我希望它能帮助到别人。无论如何,谢谢。