Google ドライブのクロス クライアント ID を使用しているため、ユーザーが Android アプリにドライブへのアクセス許可を与えると、Web サーバーもユーザーの Google ドライブにアクセスできるようになります。
このためには、まず認証コードを取得する必要があります。このために、次のコードを使用しています。
private String getAccessToken(Context mContext) {
String SERVER_CLIENT_ID = "1009999994.apps.googleusrcontent.com";
String scopes = "oauth2:server:client_id:"+SERVER_CLIENT_ID+":api_scope:"+DriveScopes.DRIVE;
String accessToken = null;
try {
accessToken = GoogleAuthUtil.getToken(mContext, accountName, scopes);
Log.d("token is:", "token:"+accessToken);
} catch (UserRecoverableAuthException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (GoogleAuthException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
Logcatで得た例外は次のとおりです。
07-20 12:12:10.210: W/GLSActivity(29164): [qq] Status from wire: INVALID_SCOPE status: INVALID_SCOPE
07-20 12:12:12.453: W/System.err(29037): com.google.android.gms.auth.GoogleAuthException: INVALID_SCOPE
07-20 12:12:12.492: W/System.err(29037): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
07-20 12:12:12.492: W/System.err(29037): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
07-20 12:12:12.500: W/System.err(29037): at com.example.googledriveaccess1.MainActivity.getAccessToken(MainActivity.java:161)
07-20 12:12:12.500: W/System.err(29037): at com.example.googledriveaccess1.MainActivity.access$0(MainActivity.java:155)
ここでは、Android 側とサーバー側の両方のアクセスに同じプロジェクトを使用しています。また、Web の Client-ID も渡しています。
以前は、クロスクライアント ID を使用せず、アプリがアクセス トークンを正しく取得していました。
以前の私の範囲は
String scope = "oauth2:"+DriveScopes.DRIVE;
お返事を待って
編集:2
ここに完全なコードをアップロードしています。このコードでは、PLUS.LOGINスコープとDriveScope.Driveを使用した後、INVALID_SCOPEの問題が解決しました。
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);
}
}
}
}
現在、常に Need_Permission 例外が発生しています。常にデバイスから許可を与えていますが、機能していません。何度も何度も NEED_EXCEPTION エラーが発生します。プロジェクト CLIENT_ID を入力して、私のコードを試すこともできます。私はこの時点で本当に立ち往生しています。
私が与えなければならない許可がこれ以上あるかもしれませんが、今はどの許可を与えるかはわかりません。
私の悪い英語でごめんなさい。