簡體   English   中英

Google App Engine Cloud Endpoints userId為null

[英]Google App Engine Cloud Endpoints userId is null

我正在使用本課程的(小修改)解決方法來獲取userId,如果請求是從Android客戶端發送的,則為null。

/**
 * This is an ugly workaround for null userId for Android clients.
 *
 * @param user A User object injected by the cloud endpoints.
 * @return the App Engine userId for the user.
 */
private static String getUserId(User user) {
    String userId = user.getUserId();
    if (userId == null) {
        LOG.info("userId is null, so trying to obtain it from the datastore.");
        AppEngineUser appEngineUser = new AppEngineUser(user);
        ofy().save().entity(appEngineUser).now();

        AppEngineUser savedUser = ofy().load().key(appEngineUser.getKey()).now();
        userId = savedUser.getUser().getUserId();
        LOG.info("Obtained the userId: " + userId);
    }
    return userId;
}

雖然我無法獲取userId。

INFO: Obtained the userId: null

該解決方法已經在其他項目中完美運行,因此問題一定存在於其他地方。 我的終結點api用以下范圍,clientId和受眾群體進行了注釋:

scopes = {
            Constants.EMAIL_SCOPE
    },
    clientIds = {
            Constants.API_EXPLORER_CLIENT_ID,
            Constants.WEB_CLIENT_ID,
            Constants.ANDROID_CLIENT_ID
    },
    audiences = {
            Constants.ANDROID_AUDIENCE
    }

Constants.ANDROID_AUDIENCEConstants.WEB_CLIENT_ID相同。 我沒有使用網絡客戶端,但是Google告訴我添加了網絡客戶端ID。 此客戶端ID是否需要指定重定向uri和javascript來源?

在我的Android客戶端中,我使用以下內容指定受眾。

mCredential = GoogleAccountCredential.usingAudience(
        EndpointService.this,
        "server:client_id:IDIDIDID.apps.googleusercontent.com"
);

請幫助我弄清楚這一點。

我只是了解為什么這種解決方法有效。 我需要開始一個新的對象化會話,以便不使用緩存,並且可以填充userId。

Objectify objectify = ofy().factory().begin();
AppEngineUser savedUser = objectify.load();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM