我正在编写一个应用程序,我们让用户使用Google SDK从Android应用程序登录,如下所示
gso = getGoogleSignInOptions();
mGoogleApiClient = getGoogleApiClient(this, this);
SignInButton mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
mSignInButton.setSize(SignInButton.SIZE_WIDE);
mSignInButton.setScopes(gso.getScopeArray());
mSignInButton.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
}
);
private Scope AUTH_DRIVE = new Scope("https://www.googleapis.com/auth/drive");
public GoogleSignInOptions getGoogleSignInOptions() {
Scope scope = new Scope(Scopes.PROFILE);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(Constants.serverClientID)
.requestServerAuthCode(Constants.serverClientID)
.requestScopes(scope, new Scope[]{AUTH_DRIVE})
.build();
return gso;
}
public GoogleApiClient getGoogleApiClient(AppCompatActivity activity, GoogleApiClient.OnConnectionFailedListener listener) {
this.activity = activity;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this.activity, listener)
.addApi(Auth.GOOGLE_SIGN_IN_API, getGoogleSignInOptions())
.addApi(Drive.API)
.build();
return mGoogleApiClient;
}
并像这样处理登录结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.wtf(TAG, "onactivity result");
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
Toast.makeText(getApplicationContext(), "Signed In!", Toast.LENGTH_LONG).show();
userAccount = result.getSignInAccount();
String gCode = userAccount.getServerAuthCode();
Log.wtf(TAG, "gcode = "+userAccount.getServerAuthCode());
Log.wtf(TAG, "token = "+userAccount.getIdToken());
if (userAccount != null) {
signUp(convertNull(userAccount.getDisplayName()), convertNull(userAccount.getEmail()), null, convertNull(userAccount.getId()), null, convertNull(userAccount.getId()), convertNull("Android"), convertNull(userAccount.getIdToken()), gCode);
}
} else {
Log.wtf(TAG, "msg = " + result.getStatus().getStatusMessage());
Log.wtf(TAG, result.getStatus().getStatusCode());
}
}
现在我们正在获取serverAuthCode和ID令牌,并将其上传到我们的后端服务器。
但是当我尝试使用我在谷歌网站上找到的代码访问Google Drive API时
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
APPLICATION_NAME = 'AppName'
CLIENT_SECRETS_PATH = 'client_secret.json'
CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
"drive-ruby-quickstart.yaml")
SCOPE = Google::Apis::DriveV3::AUTH_DRIVE
FOLDER_NAME = "AppName"
@service = nil
def authorize(user_id, code)
FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
authorizer = Google::Auth::UserAuthorizer.new(
client_id, SCOPE, token_store)
#user_id = 'default'
credentials = authorizer.get_credentials(user_id)
#credentials = nil
if credentials.nil?
url = authorizer.get_authorization_url(
base_url: OOB_URI)
puts "Open the following URL in the browser and enter the resulting code after authorization"
puts url
credentials = authorizer.get_and_store_credentials_from_code(user_id: user_id, code: code, base_url: OOB_URI)
else
puts "credentials is not nil"
end
credentials
end
我们收到以下错误
{
"error": "unauthorized_client",
"error_description": "Unauthorized"
}
有人可以帮我解释为什么会收到此错误?我已经敲了一个多星期了。