我正在制作一个应用程序,并将 Google Drive Android API 集成到其中。我有一个主要活动,然后是一个打开导致谷歌驱动器的片段。但是,当我尝试登录时(无论使用什么 Gmail 帐户,我都尝试过现有帐户、创建新帐户等),我收到 ConnectionResult 错误代码 17 SIGN_IN_FAILED。该应用程序已在开发者控制台中获得授权,并且 Drive API 已启用。我不知道还能做什么。
这是相关代码:
public class FileSharingFragment extends Fragment implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_file_sharing, container, false);
users = (TextView) rootView.findViewById(R.id.users);
getActivity().setTitle("Files");
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == Activity.RESULT_OK) {
mGoogleApiClient.connect();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), getActivity(), 0).show();
Log.i("file sharing fragment", "error code " + result.getErrorCode());
return;
}
try {
result.startResolutionForResult(getActivity(), REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
并从我使用的主要活动中调用它
Fragment fragment = FileSharingFragment.newInstance();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
我一生都无法弄清楚为什么它不让我登录。