Der folgende Codeschnipsel zeigt, dass es funktioniert. Eine Erklärung findet sich hier: http://kuennetht.blogspot.com/2010/08/accountmanager-dem-geheimnis-auf-der.html
1package com.thomaskuenneth;
2
3import java.io.InputStream;
4
5import org.apache.http.HttpResponse;
6import org.apache.http.client.HttpClient;
7import org.apache.http.client.methods.HttpGet;
8import org.apache.http.impl.client.DefaultHttpClient;
9
10import android.accounts.Account;
11import android.accounts.AccountManager;
12import android.accounts.AccountManagerCallback;
13import android.accounts.AccountManagerFuture;
14import android.app.Activity;
15import android.os.Bundle;
16import android.util.Log;
17import android.widget.TextView;
18
19public class TKSimpleTasks extends Activity {
20
21 private static final String TAG = TKSimpleTasks.class.getSimpleName();
22
23 @Override
24 public void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.main);
27
28 AccountManager am = AccountManager.get(this);
29 Account[] accounts = am.getAccountsByType("com.google");
30 if (accounts.length < 1) {
31 finish();
32 }
33 String authTokenType = "cl";
34 Bundle options = null;
35 am.getAuthToken(accounts[0], authTokenType, options, this,
36 new AccountManagerCallback<Bundle>() {
37
38 public void run(AccountManagerFuture<Bundle> arg0) {
39 try {
40 Bundle b = arg0.getResult();
41 String token = b
42 .getString(AccountManager.KEY_AUTHTOKEN);
43 Log.d(TAG, token);
44
45 HttpGet get = new HttpGet(
46 "https://www.google.com/calendar/feeds/default/owncalendars/full");
47 get.addHeader("Authorization", "GoogleLogin auth="
48 + token);
49
50 HttpClient httpclient = new DefaultHttpClient();
51 HttpResponse r = httpclient.execute(get);
52 InputStream is = r.getEntity().getContent();
53 StringBuilder sb = new StringBuilder();
54 int ch;
55 while ((ch = is.read()) != -1) {
56 sb.append((char) ch);
57 }
58
59 ((TextView) findViewById(R.id.tv)).setText(sb
60 .toString());
61
62 } catch (Throwable thr) {
63 Log.e(TAG, thr.getMessage(), thr);
64 }
65 }
66 }, null);
67 }
68}
2
3import java.io.InputStream;
4
5import org.apache.http.HttpResponse;
6import org.apache.http.client.HttpClient;
7import org.apache.http.client.methods.HttpGet;
8import org.apache.http.impl.client.DefaultHttpClient;
9
10import android.accounts.Account;
11import android.accounts.AccountManager;
12import android.accounts.AccountManagerCallback;
13import android.accounts.AccountManagerFuture;
14import android.app.Activity;
15import android.os.Bundle;
16import android.util.Log;
17import android.widget.TextView;
18
19public class TKSimpleTasks extends Activity {
20
21 private static final String TAG = TKSimpleTasks.class.getSimpleName();
22
23 @Override
24 public void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.main);
27
28 AccountManager am = AccountManager.get(this);
29 Account[] accounts = am.getAccountsByType("com.google");
30 if (accounts.length < 1) {
31 finish();
32 }
33 String authTokenType = "cl";
34 Bundle options = null;
35 am.getAuthToken(accounts[0], authTokenType, options, this,
36 new AccountManagerCallback<Bundle>() {
37
38 public void run(AccountManagerFuture<Bundle> arg0) {
39 try {
40 Bundle b = arg0.getResult();
41 String token = b
42 .getString(AccountManager.KEY_AUTHTOKEN);
43 Log.d(TAG, token);
44
45 HttpGet get = new HttpGet(
46 "https://www.google.com/calendar/feeds/default/owncalendars/full");
47 get.addHeader("Authorization", "GoogleLogin auth="
48 + token);
49
50 HttpClient httpclient = new DefaultHttpClient();
51 HttpResponse r = httpclient.execute(get);
52 InputStream is = r.getEntity().getContent();
53 StringBuilder sb = new StringBuilder();
54 int ch;
55 while ((ch = is.read()) != -1) {
56 sb.append((char) ch);
57 }
58
59 ((TextView) findViewById(R.id.tv)).setText(sb
60 .toString());
61
62 } catch (Throwable thr) {
63 Log.e(TAG, thr.getMessage(), thr);
64 }
65 }
66 }, null);
67 }
68}
