personFields掩码是必需的。请指定一个或多个有效路径

时间:2018-01-12 07:30:37

标签: android google-api kotlin google-people

嗨引用此link来调用people / me API并收到此错误

    01-12 12:33:12.859 22112-22285/com.nuveda.gol W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
01-12 12:33:12.859 22112-22285/com.nuveda.gol W/System.err: {
01-12 12:33:12.859 22112-22285/com.nuveda.gol W/System.err:   "code" : 400,
01-12 12:33:12.859 22112-22285/com.nuveda.gol W/System.err:   "errors" : [ {
01-12 12:33:12.859 22112-22285/com.nuveda.gol W/System.err:     "domain" : "global",
01-12 12:33:12.860 22112-22285/com.nuveda.gol W/System.err:     "message" : "personFields mask is required. Please specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.",
01-12 12:33:12.860 22112-22285/com.nuveda.gol W/System.err:     "reason" : "badRequest"
01-12 12:33:12.860 22112-22285/com.nuveda.gol W/System.err:   } ],
01-12 12:33:12.860 22112-22285/com.nuveda.gol W/System.err:   "message" : "personFields mask is required. Please specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.",
01-12 12:33:12.860 22112-22285/com.nuveda.gol W/System.err:   "status" : "INVALID_ARGUMENT"
01-12 12:33:12.860 22112-22285/com.nuveda.gol W/System.err: }

我无法设置personField。任何人都可以通过引用该文档告诉如何设置personFields。

这是我的代码:

 val SCOPE_USER_INFO = Scope("https://www.googleapis.com/auth/userinfo.profile")
        val SCOPE_USER_INFO = Scope("https://www.googleapis.com/auth/contacts.readonly")
        val SCOPE_EMAIL = Scope(Scopes.EMAIL)

        // Configure Google Sign In
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(context.getString(R.string.default_web_client_id))
                .requestProfile()
                .requestScopes(SCOPE_USER_INFO, SCOPE_EMAIL)
                .requestEmail()
                .build()

        // [END config_signin]

        mGoogleApiClient = GoogleApiClient.Builder(context)
                .enableAutoManage(context as AppCompatActivity /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build()


private fun signIn() {
        val signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
        (context as AppCompatActivity).startActivityForResult(signInIntent, RC_SIGN_IN)
    }

private fun updateUI(user: FirebaseUser?) {
        if (user != null) {
            val account : GoogleSignInAccount? = GoogleSignIn.getLastSignedInAccount(context)
            if (account != null) {
                val googleProfileAsync  = GoogleProfileAsync(context = activity, account = account.account!!)
                googleProfileAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
            }
        } else {
            signIn()
        }
    }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == RC_SIGN_IN && data != null) {

            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
            try {
                // Google Sign In was successful, authenticate with Firebase
                val account = task.getResult<ApiException>(ApiException::class.java)
                firebaseAuthWithGoogle(account)
            } catch (e: ApiException) {
                GOLLog.d("Google sign in failed" + e)
            }


        }
    }

private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
        GOLLog.d(TAG, "firebaseAuthWithGoogle:" + acct.id!!)
        showProgressDialog()
        val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(context as AppCompatActivity) { task ->
                    if (task.isSuccessful) {
                        // Sign in success, update UI with the signed-in user's information
                        val user = mAuth.currentUser
                        updateUI(user)
                    } else {
                        // If sign in fails, display a message to the user.
                        Toast.makeText(context, "Authentication failed.",
                                Toast.LENGTH_SHORT).show()
                        updateUI(null)
                    }

                    // ...
                }
    }

    private class GoogleProfileAsync internal constructor(context: LoginActivity, private val account: Account) : AsyncTask<Void, Void, List<Person>?>() {
            private val context: WeakReference<LoginActivity> = WeakReference(context)
            /** Global instance of the HTTP transport.  */
            private val HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport()
            /** Global instance of the JSON factory.  */
            private val JSON_FACTORY = JacksonFactory.getDefaultInstance()
            private val mProgressDialog: ProgressDialog =ProgressDialog(context)


            override fun onPreExecute() {
                super.onPreExecute()
                GOLLog.d("On PReExecute")
                    mProgressDialog.setMessage(context.get()?.getString(R.string.loading))
                    mProgressDialog.isIndeterminate = true
                    mProgressDialog.show()
            }

            override fun doInBackground(vararg voids: Void): List<Person>? {
                GOLLog.d("doInBackground")
                var response: List<Person>? = null
                try {


                    val scopes: MutableList<String> = mutableListOf("https://www.googleapis.com/auth/contacts.readonly"/*, "https://www.googleapis.com/auth/plus.login"*/)
                    val credential: GoogleAccountCredential = GoogleAccountCredential.usingOAuth2(
                            context.get(),
                            scopes
                    )
                    credential.selectedAccount = account

                    val service: People = People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                   .setApplicationName("REST API sample")
                   .build()


                    val connectionsResponse: ListConnectionsResponse = service
                   .people()
                   .connections()
                   .list("people/me")
                   .execute()
                    response = connectionsResponse.connections

                } catch (e: UserRecoverableAuthIOException) {
                    e.printStackTrace()
                    GOLLog.d("Authentication Failed")
                } catch (e: IOException) {
                    e.printStackTrace()
                }

                return response
            }

            override fun onPostExecute(response: List<Person>?) {
                super.onPostExecute(response)
                GOLLog.d("onPostExecute")
                mProgressDialog.hide()
                if (response != null) {
                    GOLLog.d("Response "  + response)
                }

            }
        }

2 个答案:

答案 0 :(得分:3)

似乎您需要set personFields掩码。

来自google samples

PeopleService service = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("Google Sign In Quickstart")
.build();

ListConnectionsResponse connectionsResponse = service
.people()
.connections()
.list("people/me")
.setPersonFields("names,emailAddresses")
.execute();

答案 1 :(得分:0)

这也有效......

    ListConnectionsResponse connectionsResponse = service
                    .people()
                    .connections()
                    .list("people/me")
                    .set("personFields","names,addresses,birthdays,genders,phoneNumbers,photos")
                    .execute();