2012-09-13 50 views
2

認真細緻地翻着大量的網頁(主要是矛盾)我不能寫一個簡單的Android應用程序,它用的OAuth2連接到谷歌後:目前編寫Google Tasks或Drive App的步驟是什麼?

我開始使用此頁:

https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android#account

我沒有下載最新的庫,不得不經歷很多過時的或缺少的功能:

  • GoogleAccountManager已被棄用
  • HttpTransport,AndroidHttp,AccessProtectedResource,GoogleAccessProtectedResource,JacksonFactory不會在當前庫中找到
  • CLIENT_ID的
  • 代通開發者控制檯的工作祕密
  • 代移動應用程序沒有工作,但需要

什麼是最新的描述?什麼是最新的演示?

以防萬一 - 這是我迄今爲止所做的。在processTokenReceived方法都不是的庫的一部分:

public class ActivityMain extends Activity implements DialogInterface.OnClickListener { 

    private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive"; 
    private static final String CLIENT_ID = "xxxxxxxxxxxx.apps.googleusercontent.com"; 

    private AccountManager accountManager; 
    private Account[] accounts; 
    private String authName; 
    private String authToken; 

    @Override 
    public void onClick(final DialogInterface dialogInterface, final int item) { 

     processAccountSelected(accounts[item]); 
    } 

    @Override 
    public void onCreate(final Bundle bundle) { 
     super.onCreate(bundle); 

     setContentView(R.layout.activitymain); 

     accountManager = AccountManager.get(this); 
     accounts = accountManager.getAccountsByType("com.google"); 

     if (accounts == null || accounts.length == 0) { 
      // TODO 
     } else if (accounts.length == 1) { 
      processAccountSelected(accounts[0]); 
     } else if (accounts.length > 1) { 
      showDialog(MyConstants.DIALOG_ACCOUNTCHOSER); 
     } 
    } 

    @Override 
    protected Dialog onCreateDialog(final int id) { 
     switch (id) { 
      case MyConstants.DIALOG_ACCOUNTCHOSER: 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 

       String[] names = new String[accounts.length]; 

       for (int i = 0; i < accounts.length; i++) { 
        names[i] = accounts[i].name; 
       } 

       alertDialogBuilder.setItems(names, this); 
       alertDialogBuilder.setTitle("Select a Google account"); 
       return alertDialogBuilder.create(); 
     } 

     return null; 
    } 

    private void processAccountSelected(final Account account) { 
     if (account != null) { 
      authName = account.name.toString(); 
      if (!Tools.isEmpty(authName)) { 
       Toast.makeText(this, authName, Toast.LENGTH_LONG).show(); 

       accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, null, this, 
         new AccountManagerCallback<Bundle>() { 

          public void run(final AccountManagerFuture<Bundle> future) { 
           try { 
            authToken = future.getResult().getString(
              AccountManager.KEY_AUTHTOKEN); 
            processTokenReceived(); 
           } catch (OperationCanceledException exception) { 
            // TODO 
           } catch (Exception exception) { 
            Log.d(this.getClass().getName(), exception.getMessage()); 
           } 
          } 
         }, null); 
      } 
     } 
    } 

    private void processTokenReceived() { 
     if (!Tools.isEmpty(authToken)) { 
      HttpTransport transport = AndroidHttp.newCompatibleTransport(); 
      AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
        accessToken); 
      Tasks service = new Tasks(transport, accessProtectedResource, new JacksonFactory()); 
      service.accessKey = CLIENT_ID; 
      service.setApplicationName("Google-DriveSample/1.0"); 
     } 
    } 
} 

回答

0

最近用於結合使用AccountManagergoogle-api-services library例子是updated Tasks API example。例如是例如剛搬到another package

如果你不使用Maven的像一個構建管理器,找回丟失的依賴你,你當然必須手動包括google-api-java-client libraries,提供HttpTransport

對於使用AccountManager就足夠用簡單的API訪問密鑰,在API控制檯中提供。只有Google Play Services可用,帶有證書指紋的零件纔會相關。

+0

在測試版中移動軟件包?好吧,似乎我最好等到明年;-)無論如何,接受了答案。謝謝你的幫助。 –

+1

Google Play服務現已推出 –

相關問題