我想知道如何使用Google Api访问/编辑Google SpreadSheet。我希望始终与来自许多设备的相同电子表格建立连接。我有使用AccountManager的例子,但我不应该使用用户帐户。有什么好的草皮吗?现在我有以下内容。。是这样吗?
AccountManageraccountManager=AccountManager.get(this);ArrayList googleAccounts=新的ArrayList();
// Just for the example, I am using the first google account returned.
Account account = new Account("email@gmail.com", "com.google");
// "wise" = Google Spreadheets
AccountManagerFuture<Bundle> amf = accountManager.getAuthToken(account, "wise", null, this, null, null);
try {
Bundle authTokenBundle = amf.getResult();
String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
// do something with the token
//InputStream response = sgc.getFeedAsStream(feedUrl, authToken, null, "2.1");
}
catch (Exception e) {
// TODO: handle exception
}
所需权限:
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
从表中选择所需的outh令牌类型:
http://code.google.com/intl/ja/apis/spreadsheets/faq_gdata.html#Authentication
电子表格数据API明智
代码示例:
public class OuthTokenActivity extends Activity {
String tag = "DEBUG";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AccountManager mAccountManager = AccountManager.get(this);
for (Account account : mAccountManager.getAccountsByType("com.google")) {
mAccountManager.getAuthToken(account, "wise", savedInstanceState,
this, resultCallback, null);
}
}
AccountManagerCallback<Bundle> resultCallback = new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle result = future.getResult();
String token = (String) result.get(AccountManager.KEY_AUTHTOKEN);
String name = (String) result.get(AccountManager.KEY_ACCOUNT_NAME);
Log.d(tag, String.format("name: %s, token: %s", name, token));
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
现在发布了一个API,可用于java脚本,可以在您的应用程序中运行。他们在这里的视频中展示了如何将其集成到Android应用程序中。