2013-08-11 15 views
7

Ho seguito le istruzioni in questo tutorial.L'utente è nullo in Google Cloud Api

https://developers.google.com/appengine/docs/java/endpoints/getstarted/auth

quando ho schierato il mio codice. e sono andato a testare la mia app.

con il seguente URL

http://chandru-compute.appspot.com/_ah/api/explorer

mio helloworld.greetings.multiply e helloworld.greetings.getGreeting funziona come previsto.

Ma ho problemi con il metodo helloworld.greetings.authed.

L'oggetto utente è sempre nullo.

Ecco il codice.

package com.google.devrel.samples.helloendpoints; 

import com.google.api.server.spi.config.Api; 
import com.google.api.server.spi.config.ApiMethod; 
import com.google.appengine.api.users.User; 
import com.google.appengine.api.users.UserService; 
import com.google.appengine.api.users.UserServiceFactory; 
import javax.inject.Named; 
import java.util.ArrayList; 
/** 
* Defines v1 of a helloworld API, which provides simple "greeting" methods. 
*/ 
@Api(
    name = "helloworld", 
    version = "v1", 
    clientIds = {com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID} 
) 
public class Greetings { 
    public static ArrayList<Greeting> greetings = new ArrayList<Greeting>(); 
    static { 
    greetings.add(new Greeting("hello world!")); 
    greetings.add(new Greeting("goodbye world!")); 
    } 

    public Greeting getGreeting(@Named("id") Integer id) { 
    return greetings.get(id); 
    } 

    @ApiMethod(name = "greetings.multiply", httpMethod = "post") 
    public Greeting insertGreeting(@Named("times") Integer times, Greeting greeting) { 
    Greeting response = new Greeting(); 
    StringBuilder responseBuilder = new StringBuilder(); 
    for (int i = 0; i < times; i++) { 
    responseBuilder.append(greeting.getMessage()); 
    } 
    response.setMessage(responseBuilder.toString()); 
    return response; 
    } 

@ApiMethod(name = "greetings.authed", path = "greeting/authed") 
public Greeting authedGreeting(User user) { 
    //Greeting response = new Greeting("hello " + user.getEmail()); 
    Greeting response; 
    if (user == null) { 
     UserService userService = UserServiceFactory.getUserService(); 
     User user2 = userService.getCurrentUser(); 
     String text = null; 
     if (user2 != null){ 
      text = user2.getEmail(); 
     } 
     response = new Greeting("hello world : Email2" + text); 
    } else { 
     response = new Greeting("hello world : Email " + user.getEmail()); 
    } 
    return response; 
} 

} 

risposta

1

Ho lo stesso problema. E se lanci un'eccezione OAuthRequestException e test il servizio tramite la console di API Explorer, riceverai un messaggio che dice This method requires you to be authenticated. You may need to activate the toggle above to authorize your request using OAuth 2.0. Quando si tenta di abilitare il controllo OAuth 2.0, esso richiede in una nuova finestra gli ambiti Select OAuth 2.0 e non sono stato in grado di trovare gli ambiti necessari o di capire come posso testare un servizio end-point cloud con autorizzazione dal Console di API Explorer.

+0

come va ora? Sto ricevendo lo stesso messaggio di errore. alla fine, come sei riuscito a risolvere il problema? – Timeless

6

ho avuto lo stesso problema, lo ha aiutato per me aggiungere

scopes = {"https://www.googleapis.com/auth/userinfo.email"}

nel mio Greetings@Api annotazione. Così l'intero finale @Api assomigliare

@Api(
name = "helloworld", 
version = "v1", 
clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID }, 
scopes = {"https://www.googleapis.com/auth/userinfo.email"} 
) 

quindi distribuire, ricaricare la pagina Api Explorer e girare anche sulle "richieste di Autorizzazione di che utilizzano OAuth 2.0" opzione con lo stesso scopo.

+6

Per me, ho avuto l'insieme 'scopes' ma non l'ID client di API Explorer. –

0

Prima di tutto, nell'API explorer, è necessario autenticare la richiesta con OAuth utilizzando le richieste di autorizzazione Author utilizzando OAuth 2.0 nell'interfaccia utente.

Se l'utente è ancora assegno nulla che tra gli ID cliente v'è l'ID per l'API Explorer

@Api(
    name = "myAPIName", 
    version = "v1", 
    clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID } 
) 

Questa è l'unica cosa che è necessaria per ottenere un argomento Utente non nullo.

Problemi correlati