Android Google SpreadSheet的Google API
问题描述:
我想弄清楚如何使用Google Api访问/编辑Google SpreadSheet。 我想通过许多设备始终使用同一电子表格进行连接。我使用AccountManager的例子,但我不应该使用用户帐户。有什么好的东西? 现在我已经得到以下..是正确的?Android Google SpreadSheet的Google API
AccountManager accountManager = AccountManager.get(this); ArrayList googleAccounts = new ArrayList();
// Just for the example, I am using the first google account returned.
Account account = new Account("[email protected]", "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();
}
}
};
}
+1
谢谢,但这需要用户在他的帐户中有电子表格。想象一下,您必须将应用程序商业化,并且您希望电子表格是您的服务器......您不能要求任何人在其中设置帐户。没有任何方法可以允许代码中的“盲登录”? – 2012-03-19 09:46:05
我在这里找到了一个很好的例子:http://code.google.com/p/yetanothercoder/希望这可以帮助某人 – 2012-03-14 12:03:13