2015-02-08 39 views
1

我試圖使用身份驗證來保存配置文件,但即使用戶爲空,GAE端點也不會提供401錯誤。GAE Cloud Endpoint未顯示401 auth錯誤

相反,端點響應200確定並給出默認結果。

順便說一句,這是Udacity Course的一部分。

/** 
* Creates or updates a Profile object associated with the given user 
* object. 
* 
* @param user 
*   A User object injected by the cloud endpoints. 
* @param profileForm 
*   A ProfileForm object sent from the client form. 
* @return Profile object just created. 
* @throws UnauthorizedException 
*    when the User object is null. 
*/ 

// Declare this method as a method available externally through Endpoints 
@ApiMethod(name = "saveProfile", path = "profile", httpMethod = HttpMethod.POST) 
// The request that invokes this method should provide data that 
// conforms to the fields defined in ProfileForm 

// TODO 1 Pass the ProfileForm parameter 
// TODO 2 Pass the User parameter 
public Profile saveProfile(final User user) throws UnauthorizedException { 

    String userId = null; 
    String mainEmail = null; 
    String displayName = "Your name will go here"; 
    TeeShirtSize teeShirtSize = TeeShirtSize.NOT_SPECIFIED; 

    // TODO 2 
    // If the user is not logged in, throw an UnauthorizedException 
    if(user== null){ 
     throw new UnauthorizedException("Authorization Required!"); 
    } 
    // TODO 1 
    // Set the teeShirtSize to the value sent by the ProfileForm, if sent 
    // otherwise leave it as the default value 

    // TODO 1 
    // Set the displayName to the value sent by the ProfileForm, if sent 
    // otherwise set it to null 

    // TODO 2 
    // Get the userId and mainEmail 

    // TODO 2 
    // If the displayName is null, set it to default value based on the user's email 
    // by calling extractDefaultDisplayNameFromEmail(...) 

    // Create a new Profile entity from the 
    // userId, displayName, mainEmail and teeShirtSize 
    Profile profile = new Profile(userId, displayName, mainEmail, teeShirtSize); 

    // TODO 3 (In Lesson 3) 
    // Save the Profile entity in the datastore 

    // Return the profile 
    return profile; 
} 

回答

0

不幸的是它的一個known issue。請去"Star" this issue以獲取更新時間。有一對夫婦的解決方法在第一個鏈接,繼承人從它報價:

有兩種解決方法(1)保存用戶,並從 存儲讀取回來,如果它指的是一個有效的帳戶的用戶ID將會填充 (這很糟糕,因爲您支付的每個API訪問的保存/加載/刪除成本即使很小,但明顯會有一些性能成本)和(2)您可以使用google + ID 這與用戶標識不同。