即使发放的允许后,始终得到NEED_PERMISSION异常 [英] Always getting NEED_PERMISSION Exception even after giving permission
问题描述
我使用谷歌驱动器跨客户端的身份,这样,当用户给驱动器访问权限设置为我的Android应用程序,我的Web服务器也获得访问用户谷歌驱动器。
I am using Google Drive cross client identity so that when user gives Drive access permission to my Android App, my web server also gains access to user Google Drive.
有关这个首先我必须得到一个授权code。对于这个我使用以下code:
For this firstly I have to get an Authorization code. For this I am using following code:
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中得到了:
Here is exception I got in 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和服务器端都访问同一个项目。
我也是通过客户ID为网页。
Here I am using same project for both Android and Server side access. I am also passing Client-ID for web.
我刚才用不正确的令牌使用跨客户端的身份和我的应用程序获得访问权限。
Earlier I was using not using Cross-Client Identity and my app getting access token correctly.
previously我的范围也
previously my scope was
String scope = "oauth2:"+DriveScopes.DRIVE;
等待你的答复。
编辑:2
在这里,我上传我的全code。在这个$ C后,我用PLUS.LOGIN范围以及与DriveScope.Drive $ C INVALID_SCOPE问题解决了。
Here I am uploading my full code. In this code INVALID_SCOPE problem resolved after I use PLUS.LOGIN scope as well with DriveScope.Drive.
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试试我的code。请帮我我真的停留在这一点。
Now I always got Need_Permission exception, I always gives permission from device but it's not working. Again and again NEED_EXCEPTION error comes. You can also try my code by inputting your project CLIENT_ID. Please help me I am really stuck at this point.
可能会有更多的许可,我不得不放弃,但我现在不其许可。
May be there be any more permission I have to give but I don't now which permission.
对不起我的英文不好。
推荐答案
使用 GoogleAuthUtil.getToken
来获取交换code。
Use GoogleAuthUtil.getToken
to retrieve the exchange code.
final private String CLIENT_ID = "abc123.apps.googleusercontent.com";
final private List<String> SCOPES = Arrays.asList(new String[]{
"https://www.googleapis.com/auth/plus.login",
"https://www.googleapis.com/auth/drive"
});
String scope = String.format("oauth2:server:client_id:%s:api_scope:%s", CLIENT_ID, TextUtils.join(" ", SCOPES));
String exchangeCode = GoogleAuthUtil.getToken(context, accountName, scope);
一个工作示例是<一个href=\"https://github.com/googledrive/crossclientoauth2-android\">https://github.com/googledrive/crossclientoauth2-android而且它更详细的<一个解释href=\"https://developers.google.com/drive/auth/android#cross-client_identity\">https://developers.google.com/drive/auth/android#cross-client_identity
A working sample is on https://github.com/googledrive/crossclientoauth2-android and it's more in detail explained on https://developers.google.com/drive/auth/android#cross-client_identity
这篇关于即使发放的允许后,始终得到NEED_PERMISSION异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!