Advertisement
Rizvan

Authenticate Error In Android application for uploading yout

Dec 28th, 2011
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1.  
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.provider.MediaStore;
  10. import android.provider.MediaStore.Video;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import com.google.gdata.client.*;
  19. import com.google.gdata.client.youtube.*;
  20. import com.google.gdata.data.*;
  21. import com.google.gdata.data.youtube.VideoEntry;
  22. import com.google.gdata.data.geo.impl.*;
  23. import com.google.gdata.data.media.*;
  24. import com.google.gdata.data.media.mediarss.*;
  25. import com.google.gdata.data.youtube.*;
  26. import com.google.gdata.data.extensions.*;
  27. import com.google.gdata.util.*;
  28. import java.io.ByteArrayOutputStream;
  29. import java.io.FileNotFoundException;
  30. import java.io.IOException;
  31. import java.io.File;
  32. import java.net.MalformedURLException;
  33. import java.net.URL;
  34.  
  35. import org.apache.http.auth.AuthenticationException;
  36. import gr.atc.epart.googlewrapper.youtube.R;
  37. public class YouTubeActivity extends Activity {
  38. public static final String developer_key;
  39. public static final String clientID;
  40. protected static final int CAMERA_CAPTURE = 0;
  41. YouTubeService service=null;
  42.  
  43. VideoEntry newEntry;
  44.  
  45. public static String username;
  46. public static String password;
  47. TextView text;
  48. TextView user;
  49. TextView passwd;
  50. EditText enteredName;
  51. EditText enteredPasswd;
  52.  
  53. static{
  54. /* I know these 4 values : */
  55. developer_key="";
  56. clientID="";
  57. username="";
  58. password="";
  59. }
  60.  
  61. /** Called when the activity is first created. */
  62. @Override
  63. public void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. setContentView(R.layout.login_page);
  66. text = (TextView) findViewById(R.id.textView1);
  67. user = (TextView) findViewById(R.id.textView2);
  68. passwd = (TextView) findViewById(R.id.textView3);
  69. enteredName = (EditText) findViewById(R.id.editText1);
  70. enteredPasswd = (EditText) findViewById(R.id.editText2);
  71.  
  72. ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
  73. public void onClick(View v) {
  74. setResult(RESULT_OK);
  75. finish();
  76. }
  77. });
  78.  
  79. ((Button) findViewById(R.id.button2)).setOnClickListener(new OnClickListener() {
  80. public void onClick(View v) {
  81. System.out.println("starting to connect");
  82. if(enteredName.getText()!=null)
  83. {
  84. username = enteredName.getText().toString();
  85. System.out.println("username: "+username);
  86. }
  87. if(enteredPasswd.getText()!=null)
  88. {
  89. password = enteredPasswd.getText().toString();
  90. System.out.println("password: "+password);
  91. }
  92.  
  93. Intent i = new Intent();
  94. i.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
  95. startActivityForResult(i, CAMERA_CAPTURE);
  96. //testGdata();
  97. //upload();
  98. }
  99. });
  100. Toast.makeText(this, "Authentication Successful", Toast.LENGTH_SHORT).show();
  101. //testGdata();
  102. }
  103.  
  104. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  105.  
  106. super.onActivityResult(requestCode, resultCode, data);
  107. Uri targetUri;
  108. if (resultCode == RESULT_OK){
  109. targetUri = data.getData();
  110. System.out.println("target"+targetUri);
  111. byte[] data1 = null;
  112.  
  113. //textTargetUri.setText(targetUri.toString());
  114. Bitmap bitmap;
  115. try {
  116.  
  117. bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
  118. System.out.println("bitmap"+bitmap);
  119. //targetImage.setImageBitmap(bitmap);
  120. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  121. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  122. data1 = baos.toByteArray();
  123. upload(data1);
  124. //
  125. //System.out.println("imagedispaly" +targetImage);
  126. } catch (FileNotFoundException e) {
  127. // TODO Auto-generated catch block
  128. e.printStackTrace();
  129. }
  130. }
  131. }
  132.  
  133. public boolean authenticate()
  134. {
  135. //System.out.println("Start authentication!!");
  136. boolean auth=false;
  137. System.out.println("Start authentication!!");
  138. service = new YouTubeService(clientID, developer_key);
  139.  
  140. try
  141. {
  142. service.setUserCredentials(username, password);
  143. System.out.println("service.getCookies(): "+service.getCookies());
  144. if(service.getCookies()!=null)
  145. auth=true;
  146. }
  147. catch(Exception e)
  148. {
  149. System.out.println("Exception: "+e);
  150. auth=false;
  151. }
  152.  
  153. return auth;
  154. }
  155.  
  156. private void testGdata() {
  157.  
  158. YouTubeService service = new YouTubeService(clientID, developer_key);
  159.  
  160. VideoEntry newEntry = new VideoEntry();
  161.  
  162. YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
  163. mg.setTitle(new MediaTitle());
  164. mg.getTitle().setPlainTextContent("Custom title here");
  165. mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME,
  166. "Politics"));
  167. mg.setKeywords(new MediaKeywords());
  168. mg.getKeywords().addKeyword("myImportantKeyword");
  169. mg.setDescription(new MediaDescription());
  170. mg.getDescription().setPlainTextContent("Custom description here");
  171. mg.setPrivate(false);
  172. mg.addCategory(new
  173. MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "mydevtag"));
  174. mg.addCategory(new
  175. MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME,
  176. "anotherdevtag"));
  177.  
  178. newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
  179. // alternatively, one could specify just a descriptive string
  180. // newEntry.setLocation("Mountain View, CA");
  181.  
  182. //MediaFileSource ms = new MediaFileSource(new File("videofile.3gp"),"video/quicktime");
  183. //MediaFileSource ms = new MediaFileSource(video, "video/3gpp");
  184. File n = new File("videofile.3gp");
  185. MediaFileSource ms = new MediaFileSource(n ,"video/3gp");
  186. newEntry.setMediaSource(ms);
  187.  
  188. String uploadUrl = "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";
  189.  
  190. try {
  191. VideoEntry createdEntry = service.insert(new URL(uploadUrl),
  192. newEntry);
  193. } catch (MalformedURLException e) {
  194. // TODO Auto-generated catch block
  195. e.printStackTrace();
  196. } catch (IOException e) {
  197. // TODO Auto-generated catch block
  198. e.printStackTrace();
  199. } catch (ServiceException e) {
  200. // TODO Auto-generated catch block
  201. e.printStackTrace();
  202. }
  203. }
  204.  
  205. public void upload(byte[] data)
  206. {
  207. boolean auth;
  208. try{
  209. auth=authenticate();
  210. System.out.println("auth in upload: "+auth);
  211. if(auth)
  212. {
  213. Toast.makeText(YouTubeActivity.this, "Connected to youTube", Toast.LENGTH_LONG);
  214. finish();
  215. }
  216. else
  217. System.out.println("not authenticated!!!");
  218. // newEntry = new VideoEntry();
  219. // YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
  220. // mg.setTitle(new MediaTitle());
  221. // mg.getTitle().setPlainTextContent("iconApp");
  222. // mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
  223. // mg.setKeywords(new MediaKeywords());
  224. // mg.getKeywords().addKeyword("aa");
  225. // mg.getKeywords().addKeyword("bb");
  226. // mg.setDescription(new MediaDescription());
  227. // mg.getDescription().setPlainTextContent("My description");
  228. // mg.setPrivate(false);
  229. // mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "mydevtag"));
  230. // mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "anotherdevtag"));
  231. //
  232. // newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
  233. // // alternatively, one could specify just a descriptive string
  234. // // newEntry.setLocation("Mountain View, CA");
  235. //
  236. // URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken");
  237. // FormUploadToken token = service.getFormUploadToken(uploadUrl, newEntry);
  238. //
  239. // System.out.println("token.getUrl() in upload: "+ token.getUrl());
  240. // System.out.println("token.getToken() in upload: "+token.getToken());
  241.  
  242. }
  243. catch(Exception e)
  244. {
  245. System.out.println("Exception: "+e);
  246. }
  247.  
  248. }
  249.  
  250. public void uploadStatus()
  251. {
  252. if(newEntry.isDraft()) {
  253. System.out.println("Video is not live");
  254. YtPublicationState pubState = newEntry.getPublicationState();
  255. if(pubState.getState() == YtPublicationState.State.PROCESSING) {
  256. System.out.println("Video is still being processed.");
  257. }
  258. else if(pubState.getState() == YtPublicationState.State.REJECTED) {
  259. System.out.print("Video has been rejected because: ");
  260. System.out.println(pubState.getDescription());
  261. System.out.print("For help visit: ");
  262. System.out.println(pubState.getHelpUrl());
  263. }
  264. else if(pubState.getState() == YtPublicationState.State.FAILED) {
  265. System.out.print("Video failed uploading because: ");
  266. System.out.println(pubState.getDescription());
  267. System.out.print("For help visit: ");
  268. System.out.println(pubState.getHelpUrl());
  269. }
  270. }
  271. }
  272.  
  273.  
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement