向 OAuth2 服务进行身份验证课程(问题)

发布于 2024-12-22 05:10:55 字数 3184 浏览 0 评论 0原文

我正在尝试遵循 developer.android.com 上的课程,并且我我被困在 am.getAuthToken( myAccount_, AUTH_TOKEN_TYPE,options,this,new OnTokenAcquired(),new Handler(new OnError()));

我不知道要为 myAccount_; 输入什么内容;是链接到帐户数组的帐户吗? Account[]accounts = accountManager.getAccountsByType("com.google");

class OnTokenAcquired 上的令牌部分也会生成一个错误,指出它不是 var,应该我只是将其设为全局变量,即使它应该是AccountManager.KEY_AUTHTOKEN 中的常量?

这是身份验证课程的另一个链接 并且我收到 DIALOG_ACCOUNTSshowDialog(DIALOG_ACCOUNTS)manager.getAuthToken(account, AUTH_TOKEN_TYPE, null, Activity, new AccountManagerCallback() 在该教程中,由于我当前遇到的错误,我没有进一步深入

。我认为只是我没有输入正确的变量,

有什么建议吗?

这是我复制的代码

public class AccountManagerActivity extends Activity {

AccountManager accountManager = AccountManager.get(this);

Account[] accounts  = accountManager.getAccountsByType("com.google");

String AUTH_TOKEN_TYPE = "Manage your tasks";
String your_api_key;
String your_client_id;
String your_client_secret;
String token;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    URL url = new URL("https://www.googleapis.com/tasks/v1/users/@me/lists?key=" + your_api_key);
    URLConnection conn = (HttpURLConnection) url.openConnection();

    conn.addRequestProperty("client_id", your_client_id);

    conn.addRequestProperty("client_secret", your_client_secret);

    conn.setRequestProperty("Authorization", "OAuth " + token);



    AccountManager am = AccountManager.get(this);
    Bundle options = new Bundle();

    am.invalidateAuthToken(token, AUTH_TOKEN_TYPE);
    am.getAuthToken(
        /*Error here*/ myAccount_,                     // Account retrieved using getAccountsByType()
        AUTH_TOKEN_TYPE,                // Auth scope
        options,                        // Authenticator-specific options
        this,                           // Your activity
        new OnTokenAcquired(),          // Callback called when a token is successfully acquired
        new Handler(new OnError()));    // Callback called if an error occurs
}

}

public class OnTokenAcquired implements AccountManagerCallback<Bundle> {

public void run(AccountManagerFuture<Bundle> result) {
    // TODO Auto-generated method stub
    // Get the result of the operation from the AccountManagerFuture.
    Bundle bundle = result.getResult();

    // The token is a named value in the bundle. The name of the value
    // is stored in the constant AccountManager.KEY_AUTHTOKEN.
  /*Error here*/  Token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
    Intent launch = (Intent) result./*Error here*/get(AccountManager.KEY_INTENT);
    if (launch != null) {
       /*Error here*/ startActivityForResult(launch, 0);
        return;
    }


}

}

I am trying to follow the lesson on developer.android.com and I am getting stuck on the
am.getAuthToken(
myAccount_, AUTH_TOKEN_TYPE,options,this,new OnTokenAcquired(),new Handler(new OnError()));

I don't get what to put in for the myAccount_; Is it accounts that is linked to the account array? Account[] accounts = accountManager.getAccountsByType("com.google");

The token part on class OnTokenAcquired is also gennerating an error saying it isn't a var, should I just make it a global var even though it is suposse to be a constant in the AccountManager.KEY_AUTHTOKEN?

This is the other link for the Authentication lesson and I am getting an error with DIALOG_ACCOUNTS, showDialog(DIALOG_ACCOUNTS) and manager.getAuthToken(account, AUTH_TOKEN_TYPE, null, activity, new AccountManagerCallback<Bundle>() in that tutorioul. I haven't gone much further in it because of the errors I am currently getting.

I don't get why these errors are happening? I assume it is just me not putting in the right vars though.

Any suggestions?

Here is the code I have copied.

public class AccountManagerActivity extends Activity {

AccountManager accountManager = AccountManager.get(this);

Account[] accounts  = accountManager.getAccountsByType("com.google");

String AUTH_TOKEN_TYPE = "Manage your tasks";
String your_api_key;
String your_client_id;
String your_client_secret;
String token;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    URL url = new URL("https://www.googleapis.com/tasks/v1/users/@me/lists?key=" + your_api_key);
    URLConnection conn = (HttpURLConnection) url.openConnection();

    conn.addRequestProperty("client_id", your_client_id);

    conn.addRequestProperty("client_secret", your_client_secret);

    conn.setRequestProperty("Authorization", "OAuth " + token);



    AccountManager am = AccountManager.get(this);
    Bundle options = new Bundle();

    am.invalidateAuthToken(token, AUTH_TOKEN_TYPE);
    am.getAuthToken(
        /*Error here*/ myAccount_,                     // Account retrieved using getAccountsByType()
        AUTH_TOKEN_TYPE,                // Auth scope
        options,                        // Authenticator-specific options
        this,                           // Your activity
        new OnTokenAcquired(),          // Callback called when a token is successfully acquired
        new Handler(new OnError()));    // Callback called if an error occurs
}

}

And then the OnTokenAcquired class

public class OnTokenAcquired implements AccountManagerCallback<Bundle> {

public void run(AccountManagerFuture<Bundle> result) {
    // TODO Auto-generated method stub
    // Get the result of the operation from the AccountManagerFuture.
    Bundle bundle = result.getResult();

    // The token is a named value in the bundle. The name of the value
    // is stored in the constant AccountManager.KEY_AUTHTOKEN.
  /*Error here*/  Token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
    Intent launch = (Intent) result./*Error here*/get(AccountManager.KEY_INTENT);
    if (launch != null) {
       /*Error here*/ startActivityForResult(launch, 0);
        return;
    }


}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2024-12-29 05:10:55
am.invalidateAuthToken(token, AUTH_TOKEN_TYPE);

应该是

am.invalidateAuthToken(AUTH_TOKEN_TYPE, token);
am.invalidateAuthToken(token, AUTH_TOKEN_TYPE);

should be

am.invalidateAuthToken(AUTH_TOKEN_TYPE, token);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文