1

I want to integrate google photos api in android,I have done google sign in process but I am not able to get access token because i was getting error in FixedCredentialsProvider.create method while passing parameter.

PhotosLibrarySettings settings =
     PhotosLibrarySettings.newBuilder()
    .setCredentialsProvider(
        FixedCredentialsProvider.create(/ Add credentials here. /)) 
    .build();

try (PhotosLibraryClient photosLibraryClient =
    PhotosLibraryClient.initialize(settings)) {

    // Create a new Album  with at title
    Album createdAlbum = photosLibraryClient.createAlbum("My Album");

    // Get some properties from the album, such as its ID and product URL
    String id = album.getId();
    String url = album.getProductUrl();

} catch (ApiException e) {
    // Error during album creation
}
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • Take a look at this answer from Jan-Felix Schmakeit who's working on the Google Photos API: https://stackoverflow.com/questions/52762861/access-google-photos-api-via-java/52826328#52826328 As far as I know there is no one-liner to get the credentials, I quoted another SO post on Jan-Felix answer if you need more information on how to build the UserCredentials – Gauthier Jan 18 '19 at 15:32
  • Thank you for your suggestion,i have already tried this code but still i am getting null in Credential and app is getting crash so please help me out this. – kuldeep pandit Jan 21 '19 at 06:16

1 Answers1

2

I was able to get a solutions to the issue

UserCredentials.newBuilder()
            .setClientId("your client id")
            .setClientSecret("your client secret")
            .setAccessToken("Access Token")
            .build()

You can pass this UserCredentials Object to ` FixedCredentialsProvider.create())

Get access token using

val client = OkHttpClient()
            val requestBody = FormEncodingBuilder()
                    .add("grant_type", "authorization_code")
                    .add("client_id", "")
                    .add("client_secret", "")
                    .add("redirect_uri", "")
                    .add("code", "yourweb server id)
                    .build()
            val request = Request.Builder()
                    .url("https://www.googleapis.com/oauth2/v4/token")
                    .post(requestBody)
                    .build()
            client.newCall(request).enqueue(object : Callback {
                override fun onFailure(request: Request, e: IOException) {

                }

                @Throws(IOException::class)
                override fun onResponse(response: Response) {
                    try {
                        val jsonObject = JSONObject(response.body().string())

                        mTokenExpired = SystemClock.elapsedRealtime() + jsonObject.optLong("expires_in") * 1000

                        accessTokenObservable.postValue(Resource.success("",jsonObject.optString("access_token")))


                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }

                }
            })

https://github.com/erickogi/AndroidGooglePhotosApi

Hope it helps

Eric Kogi
  • 89
  • 1
  • 6