Google クロス クライアント ID を使用しています。これのために私が従った手順は次のとおりです。
a) Google API コンソールでプロジェクトを作成します。b) インストールされたアプリ (Android) のクライアント ID を生成します。 c) Web アプリケーションのクライアント ID を生成します。d) ユーザーが Android 側で確認した後、サーバー側 (オフライン) でユーザーの Google ドライブを使用する必要があります。
このために、次のようなスコープを作成します。
String CLIENT_ID = "60000000007.apps.googleusercontent.com"; String scope = "server:client_id:"+CLIENT_ID+":api_scope:"+DriveScopes.DRIVE+" "+" https://www.googleapis.com/auth/plus.login ";
次に、サーバー側に転送する必要がある認証コードを取得する必要があります。このため、ここに私の完全なソースコードがあります:
package com.example.googleaccess;
import java.io.IOException;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.plus.PlusClient;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.services.drive.DriveScopes;
public class MainActivity extends Activity {
private String accountName = null;
private GoogleAccountCredential credential;
private int REQUEST_ACCOUNT_PICKER = 2;
private int REQUEST_AUTHORIZATION = 11;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String CLIENT_ID = "60000000007.apps.googleusercontent.com";
String scope = "server:client_id:"+CLIENT_ID+":api_scope:"+DriveScopes.DRIVE+" "+"https://www.googleapis.com/auth/plus.login";
credential = GoogleAccountCredential.usingOAuth2(MainActivity.this, scope);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return false;
}
class Async extends AsyncTask<Void, Void, Void> {
Context credential = null;
public Async(Context credential) {
this.credential = credential;
}
@Override
protected Void doInBackground(Void... params) {
getAccessToken(credential);
return null;
}
}
public void getAccessToken(Context mContext) {
try {
String token = credential.getToken();
Log.d("Token", "token:"+token);
} catch (GooglePlayServicesAvailabilityException playEx) {
playEx.getMessage();
playEx.printStackTrace();
}catch (UserRecoverableAuthException e) {
e.printStackTrace();
Log.d("Token", "token:"+e.getCause());
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
Log.d("Token", "token:"+e.getMessage());
} catch (GoogleAuthException e) {
e.printStackTrace();
Log.d("Token", "token:"+e.getMessage());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ACCOUNT_PICKER) {
if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
credential.setSelectedAccountName(accountName);
new Async(getApplicationContext()).execute();
}
}
}
if(requestCode == REQUEST_AUTHORIZATION) {
if (resultCode == Activity.RESULT_OK) {
data.getExtras();
new Async(getApplicationContext()).execute();
} else {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
}
}
}
ここでは、ユーザーがアプリを確認できるように UserRecoverableAuthException を処理しています。
このコードではすべて問題なく動作し、データへのアクセス許可を求められたときに、デバイスを介して許可します。しかし、再び、同じ例外を介して常に getToken() としてアプリを確認する画面が表示されます。自分のどこが欠けているのかわからない。
また、スコープを使用してアクセストークンのみを取得する必要がある場合、私のコードは正常に機能します。
String scope = "oauth2:"+DriveScopes.DRIVE;
私が直面している奇妙な問題もあります。このコードは、エミュレーター 4.2 Google API 17 バージョンで正常に機能します。しかし、どのデバイスでもありません。
このコードの Logcat は次のとおりです。
07-23 18:03:36.992: W/GLSActivity(9639): [qq] Status from wire: NeedPermission status: NEED_PERMISSION
07-23 18:03:37.414: W/System.err(9626): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
07-23 18:03:37.414: W/System.err(9626): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
07-23 18:03:37.414: W/System.err(9626): at com.example.googleaccess.MainActivity.getAccessToken(MainActivity.java:84)
07-23 18:03:37.414: W/System.err(9626): at com.example.googleaccess.MainActivity$Async.doInBackground(MainActivity.java:67)
07-23 18:03:37.421: W/System.err(9626): at com.example.googleaccess.MainActivity$Async.doInBackground(MainActivity.java:1)
07-23 18:03:37.421: W/System.err(9626): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-23 18:03:37.421: W/System.err(9626): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)