Lorenzo Sciuto
Lorenzo Sciuto

Reputation: 1685

Android Google Api for SpreadSheet

I'm trying to figure out how to use Google Api for accessing/editing Google SpreadSheet. I want to have a connection always with the same spreadsheet from many devices. I got examples using the AccountManager, but i should not use the user account. There is any good turorial? Right now i've got the following..is that right?

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@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
    }

Upvotes: 2

Views: 1610

Answers (2)

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39728

There is an API released now, available for java script, which could be run in your app. And they show how to integrate this into an Android app in a video here.

Upvotes: 1

Foobnix
Foobnix

Reputation: 759

Required permissions:

<uses-permission android:name="android.permission.ACCOUNT_MANAGER"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>

Choose needed outh token type from the table:

http://code.google.com/intl/ja/apis/spreadsheets/faq_gdata.html#Authentication

Spreadsheets Data API wise

Code sample:

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();
        }
    }
};

}

Upvotes: 2

Related Questions