2013-06-03 16 views
8

Sto provando a scrivere una semplice riga di comando google drive api in Go. Mi è sembrato finora di riuscire ad autenticare l'applicazione, dato che posso ottenere access_token e refresh_token. Il problema si verifica quando si tenta di accedere alla API SDK utilizzando il token, ottengo il seguente messaggio di erroreLimite giornaliero per l'utilizzo non autenticato superato

{ 
"error": { 
"errors": [ 
{ 
    "domain": "usageLimits", 
    "reason": "dailyLimitExceededUnreg", 
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", 
    "extendedHelp": "https://code.google.com/apis/console" 
} 
], 
"code": 403, 
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." 
} 
} 

Un'altra cosa strana che ho notato è che non vedo alcuna informazione quota nella mia console API di Google. Quindi non sono sicuro se questo è il problema. Ma dal momento che posso essere autenticato, suppongo che dovrei andare bene in termini di configurazione della console.

Di seguito è riportato il codice per la query api

accessUrl := "https://www.googleapis.com/drive/v2/files" + "?access_token=\"" + accessToken + "\"" 
if res , err := http.Get(accessUrl); err == nil { 
     if b, err2 := ioutil.ReadAll(res.Body); err2 == nil { 
      fmt.Println(string(b)) 
     }else{ 
      fmt.Println(err2) 
     } 
}else{ 
    fmt.Println(err) 
} 
+3

Ok, sono riuscito a risolvere il problema. Sembra che ho fatto lo stesso errore di molte altre persone. Ho dimenticato di abilitare "Drive API" nella console API. Una volta che l'ho fatto, allora ha funzionato bene. Il messaggio di errore è davvero fuorviante. Spero che questo aiuti qualcuno a trovare la soluzione, – tabiul

+0

, aiuta! :) –

+1

dov'è l'API di Drive? Come posso abilitare? – sureshvv

risposta

4

Questo è accaduto a me, perché uno:

  1. non ho avuto un token di aggiornamento e stavo cercando di aggiornare.
  2. Il mio token era scaduto, nel qual caso avevo bisogno di un token di aggiornamento per ottenere un nuovo token di accesso.
  3. Oppure, infine, ho avuto un token di aggiornamento, ma inavvertitamente ho dovuto scadere revocando l'accesso in modo da poter eseguire test sul sito live rispetto al sito di test.

Quindi, per prima cosa controlla che il tuo token non sia scaduto, il valore predefinito è 3600 secondi o un'ora, se non sei sicuro di poter sempre aggiornare il token.

E ricorda che una volta che un'app è stata autorizzata, le richieste successive al server non restituiranno un token di aggiornamento, che a mio parere è un po 'sciocco, ma a prescindere che sia così. Quindi la prima autenticazione è possibile ottenere un token di aggiornamento, rispetto alle richieste successive che non è possibile.

Il mio codice per ottenere un nuovo token di accesso utilizzando un token di aggiornamento è simile al seguente:

public static String refreshtoken(String refreshToken, SystemUser pUser) throws IOException { 
    HttpParams httpParams = new BasicHttpParams(); 
    ClientConnectionManager connectionManager = new GAEConnectionManager(); 
    HttpClient client = new DefaultHttpClient(connectionManager, httpParams); 
    HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token"); 

    List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
    pairs.add(new BasicNameValuePair("refresh_token", refreshToken)); 
    pairs.add(new BasicNameValuePair("client_id", "YOUR_CLIENT_ID")); 
    pairs.add(new BasicNameValuePair("client_secret", "YOUR_CLIENT_SECRET")); 
    pairs.add(new BasicNameValuePair("grant_type", "refresh_token")); 

    post.setEntity(new UrlEncodedFormEntity(pairs)); 
    org.apache.http.HttpResponse lAuthExchangeResp = client.execute(post); 
    String responseBody = EntityUtils.toString(lAuthExchangeResp.getEntity()); 
    ObjectMapper mapper = new ObjectMapper(); // can reuse, share 
               // globally 
    Map<String, Object> userData = mapper.readValue(responseBody, Map.class); 

    String access_token = (String) userData.get("access_token"); 
    String token_type = (String) userData.get("token_type"); 
    String id_token = (String) userData.get("token_type"); 
    String refresh_token = (String) userData.get("refresh_token"); 

    return access_token; 

} 

Sto usando Google App Engine e di conseguenza è necessario utilizzare GAEConnectionManager, si ottiene quei dettagli qui: http://peterkenji.blogspot.com/2009/08/using-apache-httpclient-4-with-google.html.

Spero che questo aiuti!

Problemi correlati