Android Youtube Upload Video with Static Username and Password

android youtube upload video with static username and password

I have uploaded youtube video successfully.

  • Record video and upload
  • Videos from sdcard

by using this

steps:

1) Download ytd-android-0.2.tar.gz from this link

2) Import as an android project.

3) Replace file: GlsAuthorizer.java with ClientLoginAuthorizer.java

4) Set Username and password in ClientLoginAuthorizer.java

5) Register your email id with www.appspot.com

6) Set all values in string.xml file

7) Run project..... here you go.

code for ClientLoginAuthorizer.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

/**
* Created by IntelliJ IDEA. User: jarekw Date: Nov 18, 2010 Time: 11:21:36 PM
* To change this template use File | Settings | File Templates.
*/
public class ClientLoginAuthorizer implements Authorizer {
public static final String YOUTUBE_AUTH_TOKEN_TYPE = "youtube";
private static final String AUTH_URL = "https://www.google.com/accounts/ClientLogin";
private Context ctx;
private static final String LOG_TAG = ClientLoginAuthorizer.class
.getSimpleName();

public ClientLoginAuthorizer(Context context) {
this.ctx = context;

}

@Override
public void fetchAccounts(AuthorizationListener<String[]> listener) {
// not used

}

@Override
public void addAccount(Activity activity,
AuthorizationListener<String> listener) {
// not used

}

@Override
public void fetchAuthToken(String accountName, Activity activity,
AuthorizationListener<String> listener) {
Log.d(LOG_TAG, "Getting " + YOUTUBE_AUTH_TOKEN_TYPE + " authToken for "
+ accountName);
try {
String token = getCLAuthToken(accountName);
listener.onSuccess(token);
} catch (Exception e) {

listener.onError(e);

}
}

@Override
public String getAuthToken(String accountName) {
try {
String token = getCLAuthToken(accountName);
return token;
} catch (IOException e) {

e.printStackTrace();
return null;

}
}

public String getCLAuthToken(String accountName) throws IOException {
HttpURLConnection urlConnection = getGDataUrlConnection(AUTH_URL);
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
String template = "Email=%s&Passwd=%s&service=%s&source=%s";
String userName = "USERNAME"; // TODO
String password = "PASSWORD"; // TODO
String service = YOUTUBE_AUTH_TOKEN_TYPE;
String source = ctx.getString(R.string.client_id);
String loginData = String.format(template, encode(userName),
encode(password), service, source);
OutputStreamWriter outStreamWriter = new OutputStreamWriter(
urlConnection.getOutputStream());
outStreamWriter.write(loginData);
outStreamWriter.close();
int responseCode = urlConnection.getResponseCode();
if (responseCode != 200) {
Log.d(LOG_TAG, "Got an error response : " + responseCode + " "
+ urlConnection.getResponseMessage());
throw new IOException(urlConnection.getResponseMessage());
} else {

InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (line.startsWith("Auth=")) {
String split[] = line.split("=");
String token = split[1];
Log.d(LOG_TAG, "Auth Token : " + token);
return token;
}
}
}

throw new IOException("Could not read response");

}

private String encode(String string) throws UnsupportedEncodingException {
return URLEncoder.encode(string, "UTF-8");

}

private HttpURLConnection getGDataUrlConnection(String urlString)
throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
return connection;

}

@Override
public String getFreshAuthToken(String accountName, String authToken) {
return getAuthToken(accountName);

}

public static class ClientLoginAuthorizerFactory implements
AuthorizerFactory {
public Authorizer getAuthorizer(Context context, String authTokenType) {
return new ClientLoginAuthorizer(context);

}
}
}

Uploading video to youtube API V3 without prompting user login

I have decided to answer my own question after many hours searching for a solution ,Actually there is no proper way of uploading a video to Youtube using API V3 without prompting user's login,This was possible using API V2 which is no longer supported,For that case you wont be able to declare your static username and password.It is advised to use server-side language like PHP to do this for you.You can please refer on this question on how to do that.

android - upload videos to fixed YouTube account without using Google OAuth 2.0

Finally I found the solution to my problem. Now I am able to upload videos to a static YouTube account from my android app. although Youtube Data api v2 is deprecated but I used this for my requirement.
The following code I am using to upload videos to YouTube

    YouTubeService service = new YouTubeService("project id on console.developer.google.com","androidkey");
service.setUserCredentials("yourYouTubeAccount@gmail.com", "yourPassword");
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent("Video Title");
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Tech"));
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("anyKeyword");
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent("VIDEO DESCRIPTION");
mg.setPrivate(false);
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "mydevtag"));
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "anotherdevtag"));
MediaFileSource ms = new MediaFileSource(videoFileToUpload, "video/quicktime");
newEntry.setMediaSource(ms);
VideoEntry createdEntry = service.insert(new URL(Constant.YOUTUBE_UPLOAD_URL), newEntry);
Log.v("TAG", "VIDEO INSERTED ID : " + createdEntry.getId());

You will require the following Libraries to use the code:

  • activation.jar
  • additionnal.jar
  • gdata-base-1.0.jar
  • gdata-core-1.0.jar
  • gdata-media-1.0.jar
  • gdata-youtube-2.0.jar
  • gdata-youtube-meta-2.0.jar
  • guava-11.0.2.jar
  • mail.jar
  • servlet-api.jar

for gdata libraries please go to this link and download both gdata-src.java-x.xx.x.zip and gdata-samples.java-x.x.x.zip. Extract these folders and you will get the required jars

-Thanks



Related Topics



Leave a reply



Submit