Gdata make own API

Put your problem here if it does not fit any of the other categories.

Gdata make own API

Postby MickArea » Sat Feb 28, 2009 9:47 pm

Hi, i try to make my own API because the official one doesn't work on android.
So i make that :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class api_calendar {
  2.  
  3.         private DefaultHttpClient httpclient;
  4.         private HttpPost httpost;
  5.         private HttpResponse response;
  6.  
  7.         String username="test@gmail.com";
  8.         String password="password";
  9.         private String Auth;
  10.         private BasicHeader header;
  11.         private HttpGet httpget;
  12.  
  13.         public api_calendar() {
  14.                 httpclient = new DefaultHttpClient();
  15.                 httpost = new HttpPost("https://www.google.com/accounts/ClientLogin");
  16.         }
  17.  
  18.         public boolean connect() {
  19.                 List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  20.                 nvps.add(new BasicNameValuePair("Email", username));
  21.                 nvps.add(new BasicNameValuePair("Passwd", password));
  22.                 nvps.add(new BasicNameValuePair("source", "MyApiV1"));
  23.                 nvps.add(new BasicNameValuePair("service", "cl"));
  24.                 try {
  25.  
  26.                         httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.DEFAULT_CONTENT_CHARSET));
  27.                         response = httpclient.execute(httpost);
  28.                         if( response.getStatusLine().getStatusCode()!=200 )return false;
  29.                         convertStreamToData(response.getEntity().getContent());
  30.  
  31.                         header=new BasicHeader("Authorization","AuthSub token=""+Auth+""");
  32.  
  33.                         String feedUrl = "http://www.google.com/calendar/feeds/""+username+""/private/full";
  34.                         httpget = new HttpGet(feedUrl);
  35.  
  36.                         httpget.addHeader(header);
  37.  
  38.                         response = httpclient.execute(httpget);
  39.                         Log.d("test","test "+convertStreamToDisplay(response.getEntity().getContent()) ); // print
  40.  
  41.  
  42.                 } catch (UnsupportedEncodingException e) {
  43.                         e.printStackTrace();
  44.                         return false;
  45.                 } catch (ClientProtocolException e) {
  46.                         e.printStackTrace();
  47.                         return false;
  48.                 } catch (IOException e) {
  49.                         e.printStackTrace();
  50.                         return false;
  51.                 }
  52.  
  53.  
  54.  
  55.                 return true;
  56.  
  57.         }
  58.  
  59.         public void convertStreamToData(InputStream is) {
  60.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  61.                
  62.  
  63.                 String line = null;
  64.                 try {
  65.                         while ((line = reader.readLine()) != null) {
  66.                                 Log.d("line ", ": "+line);
  67.                                 if( line.startsWith("Auth=")) {
  68.                                         Auth=line.substring(5);
  69.                                         Log.i("auth",": "+Auth);
  70.                                 }
  71.                         }
  72.                 } catch (IOException e) {
  73.                         e.printStackTrace();
  74.                 } finally {
  75.                         try {
  76.                                 is.close();
  77.                         } catch (IOException e) {
  78.                                 e.printStackTrace();
  79.                         }
  80.                 }
  81.  
  82.                
  83.         }
  84.         public String convertStreamToDisplay(InputStream is) {
  85.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  86.                 StringBuilder sb = new StringBuilder();
  87.  
  88.                 String line = null;
  89.                 try {
  90.                         while ((line = reader.readLine()) != null) {
  91.                                 sb.append(line + "n");
  92.                         }
  93.                 } catch (IOException e) {
  94.                         e.printStackTrace();
  95.                 } finally {
  96.                         try {
  97.                                 is.close();
  98.                         } catch (IOException e) {
  99.                                 e.printStackTrace();
  100.                         }
  101.                 }
  102.  
  103.                 return sb.toString();
  104.         }
  105. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


but i can get the auth token, but it doesn't work, i don't know why ?
i get this message :
02-28 21:46:32.971: INFO/global(707): INFO: Default buffer size used in BufferedReader constructor. It would be better to be explicit if a 8k-char buffer is required.
02-28 21:46:32.981: DEBUG/line(707): : SID=DQAAAIoAAAAGWjrz5Yx8KcGoLEEW8O3UY1J53CmlZrTMuh4qPfyfUrce2haJ4yb0k237gdj3ttPTnybb-lQrvC8EjEitTmace1C9GkSFeaksU0ls85ukNCPo83LDRC9LOxki4SfoYHS4uSyVsYYAXzzzkZmuk3hWjImJ45gQXIJiPWIk3neEqEPUgYrBbwBYNHmWqnA6VS8
02-28 21:46:32.991: DEBUG/line(707): : LSID=DQAAAIsAAACITL1nsIewqfX0ThJzUtcvoX4P52hFIoMdq-LtA45lfjMlBCQDiOBuPW5_G3IUq55uoAu7G1YbY4F2EWMkicIbVHaso5QY1LhTpZYpYZ1LROgSV1pAazrzJ67te-oht0-ISsZL7uJKhQciT5dXj_6BdTZUUCOznjoKMVjkfzHucYXDJQuAamJ08k4eh_avKaY
02-28 21:46:32.991: DEBUG/line(707): : Auth=DQAAAIsAAACITL1nsIewqfX0ThJzUtcvoX4P52hFIoMdq-LtA45lfjMlBCQDiOBuPW5_G3IUq57bCy-m4kWICKyesWRpP4hUWFgIK3nh907a9shtERYGiVNtbhjUCvEclHym6OlPQxlbuNyzTHj5uKyitQgZ3cGtOcFa4HncRkKRy0G9l21kBF7PriSJk1wE2KqBOr6PMsI
02-28 21:46:33.001: INFO/auth(707): : DQAAAIsAAACITL1nsIewqfX0ThJzUtcvoX4P52hFIoMdq-LtA45lfjMlBCQDiOBuPW5_G3IUq57bCy-m4kWICKyesWRpP4hUWFgIK3nh907a9shtERYGiVNtbhjUCvEclHym6OlPQxlbuNyzTHj5uKyitQgZ3cGtOcFa4HncRkKRy0G9l21kBF7PriSJk1wE2KqBOr6PMsI
02-28 21:46:33.142: DEBUG/dalvikvm(707): GC freed 1410 objects / 90928 bytes in 74ms
02-28 21:46:33.302: INFO/global(707): Feb 28, 2009 9:46:33 PM java.io.BufferedReader <init>
02-28 21:46:33.302: INFO/global(707): INFO: Default buffer size used in BufferedReader constructor. It would be better to be explicit if a 8k-char buffer is required.
02-28 21:46:33.312: DEBUG/test(707): test <HTML>
02-28 21:46:33.312: DEBUG/test(707): <HEAD>
02-28 21:46:33.312: DEBUG/test(707): <TITLE>Token invalid - Invalid AuthSub token.</TITLE>
02-28 21:46:33.312: DEBUG/test(707): </HEAD>
02-28 21:46:33.312: DEBUG/test(707): <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
02-28 21:46:33.312: DEBUG/test(707): <H1>Token invalid - Invalid AuthSub token.</H1>
02-28 21:46:33.312: DEBUG/test(707): <H2>Error 401</H2>
02-28 21:46:33.312: DEBUG/test(707): </BODY>
02-28 21:46:33.312: DEBUG/test(707): </HTML>

MickArea
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Feb 23, 2009 4:42 pm

Top

Postby MickArea » Sun Mar 01, 2009 12:39 am

finaly i found my way, the error is due to a wrong header :

header=new BasicHeader("Authorization","GoogleLogin auth="+Auth);

this one work ^^
cool
MickArea
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Feb 23, 2009 4:42 pm

Postby DavidLanz » Thu Mar 12, 2009 8:28 am

[quote="MickArea"]finaly i found my way, the error is due to a wrong header :

header=new BasicHeader("Authorization","GoogleLogin auth="+Auth);

this one work ^^
cool[/quote]


I got a problem, even when I got "Auth" from InputStream
then I try to create CalendarService object below:

CalendarService myService = new CalendarService("exampleCo-exampleApp-1.0");
myService.setAuthSubTokenAuth
URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);

it throw an exception:

com.google.gdata.util.AuthenticationException: Token invalid - Invalid AuthSub token

how can I set AuthSub Token to my CalendarService object ?
DavidLanz
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Dec 23, 2008 3:31 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 10 guests