Jump to content

Authentification requête http Get


Ploue

Recommended Posts

Bonjour.

Je réalise actuellement une application dans laquelle je voudrais récupérer des informations d'un compte google (gmail plus particulièrement), et j'ai un petit souci concernant une requête http.

J'ai commencé par faire une requête de login (en POST), afin de récupérer une authentification google. Celle-ci semble fonctionner, j'ai un code retour 200 et un token "Auth". J'ai ensuite réalisé une requête pour récupérer les données à propos des contacts google (en GET), mais je ne sais pas comment transmettre l'autorisation obtenue lors du login.

Voici ce que j'ai tenté :

   public static String login() {
       String url = "https://www.google.com/accounts/ClientLogin";
       String auth = "";
       BufferedReader bufferedReader = null;

       try {
           HttpClient httpClient = new DefaultHttpClient();
           HttpPost httpPost = new HttpPost();

           URI uri = new URI(url);
           httpPost.setURI(uri);

           List nameValuePairs = new ArrayList();  
           nameValuePairs.add(new BasicNameValuePair("accountType", "HOSTED_OR_GOOGLE"));  
           nameValuePairs.add(new BasicNameValuePair("Email", "xxxxx@gmail.com"));  
           nameValuePairs.add(new BasicNameValuePair("Passwd", "xxxxxxxx"));  
           nameValuePairs.add(new BasicNameValuePair("service", "cp"));    // Service contact data  
           nameValuePairs.add(new BasicNameValuePair("source", "xxxxxxxxx"));  
           httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

           HttpResponse httpResponse = httpClient.execute(httpPost);

           InputStream inputStream = httpResponse.getEntity().getContent();
           bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

           // Extraction du token Auth
           String ligneCodeHTML = bufferedReader.readLine();
           while (ligneCodeHTML != null){
               if (ligneCodeHTML.startsWith("Auth"))
                   auth = ligneCodeHTML;
               ligneCodeHTML = bufferedReader.readLine();
           }
           auth = auth.substring(auth.indexOf("=") + 1);
           [...]
       return auth;
   }

public static String emailForUrl(final String email) {
       String newEmail = email;
       newEmail.replaceAll("@", "%40");
       // TODO : accents, autres caracteres spéciaux
       return newEmail;
}

public static String getGoogleContacts(String auth) {
       String monEmail = "xxxxxxxxx@gmail.com";
       String url = "http://www.google.com/m8/feeds/contacts/" + emailForUrl(monEmail) + "/full";
       BufferedReader bufferedReader = null;

       try {
           HttpClient httpClient = new DefaultHttpClient();
           HttpGet httpGet = new HttpGet();

           URI uri = new URI(url);
           httpGet.setURI(uri);

           // 1er essai :
           httpGet.setHeader("Auth", auth);
           // fin

           // 2ème essai :
           HttpParams httpParams = httpGet.getParams();
           httpParams.setParameter("Auth", auth);
           httpGet.setParams(httpParams);
           // fin

           HttpResponse httpResponse = httpClient.execute(httpGet);
           [...]
   }

J'ai aussi essayé les deux solutions avec "auth" plutôt que "Auth", car j'ai vu ici qu'il fallait mettre ceci dans la requête :

Authorization: GoogleLogin auth=yourAuthToken

J'ai une autre petite question. Je pensais par la suite utiliser OAuth plutôt que ce type d'authentification, qu'en pensez-vous ? (je sais pas du tout ce que c'est, je suis tombé dessus par hasard). C'est plus sécurisé c'est ça ? Je précise que pour l'instant le mot de passe est en clair dans mon appli, mais quand ça marchera je compte le récupérer d'un champ password.

Je vous remercie d'avance !

Link to comment
Share on other sites

Bin en tout cas la requête de login semble fonctionner, du moins j'ai un code retour 200 et les toker SID, Auth, etc. Donc à priori on peut faire comme ça, après il y a probablement plus sécurisé, c'était aussi une de mes questions.

Je suis parti de l'api pour faire cette méthode : http://code.google.com/intl/fr/apis/contacts/docs/3.0/developers_guide_protocol.html#client_login

As-tu autre chose à me proposer sinon pour effectuer une connexion plus sécurisée ?

Edit : je viens de voir que dans l'api, il y a : "ClientLogin can be used with any application that can make an HTTPS POST request." donc à priori on peut.

Edited by Ploue
Link to comment
Share on other sites

  • 5 weeks later...

Salut,

En fait j'ai le même problème depuis 3 jours et je n'arrive pas à le résoudre,donc j'ai voulu vous demandez si vous êtes parvenus à une solution ou bien une autre alternative qui permet de récupérer les données d'un compte google.

Pour la définition du header j'ai utilisé:httpget.setHeader("Authorization", "GoogleLogin auth=" + token);

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...