Java 在android应用程序中集成Google照片库API

Java 在android应用程序中集成Google照片库API,java,android,rx-java,google-photos-api,Java,Android,Rx Java,Google Photos Api,我想在android中集成google photos api,我已经完成了google登录过程,但无法获取访问令牌,因为在传递参数时,我在FixedCredentialsProvider.create方法中出错 PhotosLibrarySettings settings = PhotosLibrarySettings.newBuilder() .setCredentialsProvider( FixedCredentialsProvider.create(/

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

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
}

我找到了解决这个问题的办法

UserCredentials.newBuilder()
            .setClientId("your client id")
            .setClientSecret("your client secret")
            .setAccessToken("Access Token")
            .build()
您可以将此UserCredentials对象传递给'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()
                    }

                }
            })


希望这有帮助

看看Jan Felix Schmakeit的回答,他正在开发Google Photos API:据我所知,没有一家公司可以获得认证,我引用了Jan Felix answer上的另一篇SO帖子,如果你需要关于如何建立用户认证的更多信息,谢谢你的建议,我已经尝试过这个代码,但我仍然在凭据中得到空值,并且应用程序正在崩溃,所以请帮助我解决这个问题。