使用 Kotlin/Java 访问 Google 相册 API

问题描述

我正在尝试将我的程序与谷歌照片 API 连接起来。我需要它,以防万一使用谷歌照片 API 进行 CRUD 操作。

我尝试使用 google 照片 API documentation 中的示例代码

// Set up the Photos Library Client that interacts with the API
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
}

我正在从 Google Cloud Platform 创建凭据。当我使用服务帐户凭据执行此操作时,它显示“权限被拒绝:不支持服务帐户”。我也尝试过使用 OAuth 2.0 客户端,但又出现了一个错误——json 凭证文件中没有类型字段。

我还阅读了 Access google photos API via Java stackoverflow 问题并尝试使用该示例代码

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

这里的问题是我无法创建访问令牌。我看到了 Integrate Google Photos Library API in android application Kogi eric 的回答,他使用以下代码创建了访问令牌:

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

                }
            })

但不知道在这些字段中输入什么:

.add("grant_type","yourweb server id)

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...