Google Drive Android API ConnectionResult Error

杀马特。学长 韩版系。学妹 提交于 2019-12-12 10:49:19

问题


I'm making an app and I'm integrating the Google Drive Android API into it. I have a main activity then a fragment that opens that leads to google drive. However, when I try to sign in (it doesn't matter what gmail account, I've tried existing ones, creating new ones, whatever) I get ConnectionResult error code 17 SIGN_IN_FAILED. The app is authorized in the developer console and the Drive API is enabled. I don't know what else to do.

Here is the relevant code:

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);
    }
}

And to call it from the main activity I use

Fragment fragment = FileSharingFragment.newInstance();
FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

I cannot for the life of me figure out why it won't let me sign in.


回答1:


You probably know by now that your Developers Console needs 3 things:

1/ SHA1 of yout APK
2/ your package name
3/ your email/product name in the Consent Screen

Assuming everything is OK there, here's the last-resort double-check I use often:
- open (unzip) your xxxx.APK using unzipper (like 7zip), find CERT.RSA.
- run 'keytool -printcert -file .........\CERT.RSA'
Go back to the console and compare the SHA1's again.

Good Luck




回答2:


It might seem obvious but if you've entered your fingerprint on the developer console and set up your API there and are still getting SIGN_IN_FAILED make sure you are actually using the signed APK!

It's too easy to run installDebug from Gradle and forget. I usually set it up and go through the menus (Build > Generate Signed APK...) in Android Studio when I'm working with Google SDK's




回答3:


Over the Google Developers Console go to API & Auth -> Credentials and check the Package name is exactly the same that your Application Package name.




回答4:


Make sure that you have generated an OAuth 2.0 Client ID as opposed to a normal API key

According to Google docs for youtube API - Your application must send an OAuth 2.0 token with any request that accesses private user data




回答5:


Try uninstalling the app and starting the debug process all over (if you are using the debug.keystore.



来源:https://stackoverflow.com/questions/29689832/google-drive-android-api-connectionresult-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!