[英]Obtaining an access token in OAuth2
我正在嘗試用Java在Android設備上獲取訪問令牌。
首先,我在Google API控制台中為已安裝的應用程序(Android)創建了客戶端ID,並設置了請求訪問令牌的程序包和SHA1指紋 。
當我在OAuth2ForDevices中讀到時 ,為了獲得訪問令牌,我必須首先獲得用戶代碼。 所以我嘗試使用Aquery發送帶有client_id和范圍的POST請求,如下所示:
AQuery aq = new AQuery(activity);
aq.ajax("https://accounts.google.com/o/oauth2/device/code?" +
"scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile" +
"client_id=xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
JSONObject.class,new AjaxCallback<JSONObject>(){
@Override
public void callback(String url, JSONObject traffic_flow, AjaxStatus status) {
publishProgress(traffic_flow.toString());
}
});
問題是JSONObject traffic_flow
總是為null。 我也嘗試使用它(但我不認為這是正確的方法):
authToken = GoogleAuthUtil.getToken(activity, mEmail, "audience:server:client_id:xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com");
其中mEmail是來自android設備的電子郵件,但我得到GoogleAuthException
“Unknown”。 如何正確獲取用戶代碼?
編輯:
我終於能夠使用這個獲得一個身份驗證令牌:
String scope = "audience:server:client_id:xxxxxxxxxxxx.apps.googleusercontent.com";
String token = GoogleAuthUtil.getToken(activity, client.getAccountName(), scope);
其中范圍是在Google API控制台中生成的網絡應用程序的客戶端ID(之后我將令牌發送到我的網站並進行驗證),客戶端是PlusClient(更多關於Google+入門 )。
我在我的測試應用程序中獲得了令牌,現在的問題是,當我想將代碼包含到我的新應用程序中時,我再次得到了那個丑陋的異常:
com.google.android.gms.auth.GoogleAuthException: Unknown at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
這些應用程序的所有客戶端ID都在同一個項目中,清單中的權限應該沒問題( GET_ACCOUNTS,USE_CREDENTIALS,AUTHENTICATE_ACCOUNTS,INTERNET,ACCESS_NETWORK_STATE
)。 我在新應用程序中所做的唯一更改是在創建PlusClient時設置范圍,因為沒有它就無法工作(不知道為什么沒有它在我的測試應用程序中工作)
mPlusClient = new PlusClient.Builder(this, this, this)
.setVisibleActivities("http://schemas.google.com/AddActivity")
.setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE)
.build()
我錯過了什么?
如果您必須使用此API,那么您應該使用POST版本的AQuery並正確傳遞POST參數,如下所示。 此API OAuth2ForDevices適用於資源受限的設備,用戶可以通過其他方式授權您的應用。
params.put("scope", "your scopes");
params.put("client_id", "your client id");
AQuery aq = new AQuery(activity);
aq.ajax("https://accounts.google.com/o/oauth2/device/code", params, JSONObject.class,
new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject traffic_flow, AjaxStatus status) {
publishProgress(traffic_flow.toString());
}
});
但是,如果您的要求是使用Android上的常規OAuth2,例如具有常規輸入功能的Android手機,那么Android的標准OAuth機制就是這樣 。
這是我用於獲取與Google Coordinate API一起使用的OAuth 2.0令牌的代碼,我在ASyncTask中運行它。 我沒有包含用於生成DESIRED_USER的部分,因為它在某些AccountManager意圖上使用了startActivityForResult()。 希望你能用到這個。
您應該用您的SCOPE替換,我認為應該是oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
import android.accounts.Account;
import android.accounts.AccountManager;
final String SCOPE = "oauth2:https://www.googleapis.com/auth/coordinate";
AccountManager accountManager = AccountManager.get(context);
//Kill Current token
accountManager.invalidateAuthToken("com.google",authManager.getToken());
//Fetch userAccount
Account userAccount = null;
for(Account account : accountManager.getAccounts())
if(account.name.equals(DESIRED_USER))
{
try {
return accountManager.blockingGetAuthToken(userAccount, SCOPE, false);
} catch (Exception e) {
System.out.println("Error: "+e.toString());
e.printStackTrace();
}
}
return null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.