将Google Photos Library API集成到android应用程序中 [英] Integrate Google Photos Library API in android application

查看:95
本文介绍了将Google Photos Library API集成到android应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将google photos api集成到android中,我已经完成了google登录过程,但是我无法获取访问令牌,因为在传递参数时我在FixedCredentialsProvider.create方法中遇到了错误.

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
}

推荐答案

我能够找到解决该问题的方法

I was able to get a solutions to the issue

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

您可以将此UserCredentials对象传递给`FixedCredentialsProvider.create())

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

使用

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

希望有帮助

这篇关于将Google Photos Library API集成到android应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆