My problem: I am currently in the situation of being able to verify if the user has been correctly bound to Google Drive
. In other words, perform a link checking system. This problem arises because I have noticed that sometimes due to a lack of Internet connection or errors in the services Google
errors occur in the link and the user is not linked, and if this user is not linked, an error occurs when trying to perform another method . Example: create a folder.
My current solution was a method provided by the class GoogleApiClient
:
GoogleApiClient apiClient;
apiClient.isConnected()
This method returns if the user is connected to the services of Google
but in the plural and not in the singular, therefore it does not allow me to specifically verify Google Drive
.
If you know how to achieve my goal, I will be happy to hear or rather read your comments and answers. Thank you so much.
The Android API allows you to connect specifically to Google Drive. Although for now it has some limitations as indicated in the note below. I put in bold for now because Google says so, and we can interpret that they plan to expand the scope of the API.
How to connect and authorize Google Drive API for Android
Authorization for the Google Drive API for Android is handled by GoogleApiClient. It is usually created in the method
onCreate()
of an activity.After you create the client, you must connect it for authorization to occur:
If the user has not previously authorized the application, the callback will be invoked
onConnectionFailed()
. The callback must check for the presence of a pending Intent to resolve the error and start the activity. The user will be prompted to authorize the app to access their files in Drive.The activity method
onActivityResult()
will be called on completion. If the result isRESULT_OK
, the activity must callconnect()
again to establish the connection.Another possible solution
If the scope currently given by the Google Drive API for Android is not enough for what you want to do, you can use the Java API as you used it before, but restricting the connection only to Google Drive, that is:
a. Sending only SCOPES relative to Google Drive on the connection, or:
b. Configuring in the Google Developers Console a project that only accesses Google Drive and no other Google services and connecting with those credentials.
Thus, by verifying the connection you will be sure that you are connected only to Google Drive.
Source: Google Drive API for Android
This can be simple, your class should implement ConnectionCallbacks and OnConnectionFailedListener ,
ConnectionCallbacks has the methods:
while OnConnectionFailedListener has the method:
As an example we define a variable to store the status:
If the connection failed we can determine what the method was called:
On the other hand, if the connection was made successfully, the method is called:
If there is a connection, you can connect without problem and perform operations on your Google Drive: