recently in the research with the Youku SDK for video upload function, because the Youku SDK only provides an uploaded sample code, there is no authorization process involved. For beginners. It could be tricky. Now share my thoughts:
before the implementation of the program we need to go to the Youku open platform Download SDK (:http://open.youku.com/down) According to the Youku SDK, the upload process is divided into the following 7 steps:
1. New Android project, introducing youkuuploadsdk-android jar Package
2. In the Mainactivity.java file add code as seen in Sample.java, add the relevant parameters
3. Activity_main.xml add control under directory layout
4. Generate code (XML file code)
5. Connect Android phone to perform Android project build APK (remember to turn on developer options USB debugging, etc.)
6. Authorization process (get Access_token)
7. Click Upload Upload
Other steps are done according to the documentation and should not be problematic, the following emphasis is on the authorization process for step 6.
We are only able to use the generic authorization method (the level of cooperation requires a fee), regarding the authorization mechanism. Please refer to http://open.youku.com/docs/OAuth2.html.
The idea is that through a webview to display the authorization page to obtain the Authorization Code (code), We need to use the HTTP method get to obtain the authorization Code (code), the required number of parameters please refer to http://open.youku.com /docs/oauth2.html?id=101. Then by using the Authorization Code (code) to exchange tokens known as Access_token, and finally through the Access_token to get upload permissions.
The code looks like the following code:
Androidmanifest.xml
<?XML version= "1.0" encoding= "Utf-8"?
><manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.youku.uploader" Android: Versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "one" android:targets dkversion= "/> <uses-permission android:name=" Android.permission.INTERNET "/> <uses-permission androi D:name= "Android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses-permission android:name=" android.permission.RECORD_ AUDIO "/> <!--this permission for network positioning-<uses-permission android:name=" Android.permission.ACCESS_COARSE_LOCAT ION "/> <!--this permission to access GPS--<uses-permission android:name=" Android.permission.ACCESS_FINE_LOCATION "/ > <!--for access to WiFi network information, WiFi information is used for network positioning-<uses-permission android:name= "Android.permission.ACCESS_WIFI_S TATE "/> <!--get carrier information to support interfaces that provide carrier information--<uses-Permission Android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <!--this permission is used to get access to WiFi. WiFi information is used for network positioning-<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE"/> <!--used to read the phone when State-to-<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/> <!--write to the extended storage and write data to the expansion card. For writing offline location data--<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <!--SD card Read access , the user writes offline location data--<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <applic ation android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name "Android:theme=" @style/apptheme "> <activity android:name=" Sample. Getcodeactivity "android:label=" @string/app_name "> <intent-filter> <acti On android:name= "Android.intent.action.MAIN"/> <catEgory android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name= "Sample. Mainactivity "android:label=" @string/app_name "> </activity> </application></manife St>
Getcodeactivity.java
Package Sample;import Java.util.arraylist;import Java.util.list;import org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.message.basicnamevaluepair;import Org.apache.http.protocol.http;import Org.apache.http.util.entityutils;import Org.json.jsonobject;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.util.log;import Android.webkit.WebView;import Android.webkit.webviewclient;import Android.widget.toast;import Com.youku.uploader.r;public class GetCodeActivity Extends Activity {private WebView WebView; Private String Strcode; Private String Access_token; private string client_id = "38bae68624af4531";p rivate string client_secret = "feb939fd882b449e1f022aa12fb9b06a"; private string tag= "Getcodeactivity";p rivate string redirect_url = "httpS://CLIENT.EXAMPLE.COM/CB "; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.o Ncreate (savedinstancestate); Setcontentview (R.layout.request_code); Set Web View WebView = (WebView) Findviewbyid (R.ID.WEBVIEW1); if (WebView = = null) {return; }//The following link you can only change the value of client_id and Redirect_uri back to the one we applied for Youku developer account when Youku provided and we set the callback address to be able to String url = "https:// Openapi.youku.com/v2/oauth2/authorize?client_id= "+client_id+" &response_type=code&redirect_uri= "+ redirect_url+ "&STATE=XYZ"; Webview.loadurl (URL); Https://api.weibo.com/oauth2/default.html log.d (TAG, "Load before=" +url); Webview.setwebviewclient (New Webviewclient () {@Override public boolean Shouldoverrideurlloadi Ng (WebView view, String URL) {//TODO auto-generated method stub view.loadurl (URL); string[] str = NULL; str = url.split ("=") [1].split ("&"); Strcode = str[0]; Intent it = new Intent (); It.putextra ("code", Strcode); It.putextra ("Access_token", GetToken ()); LOG.D (TAG, "code=" +strcode+ "status=" +str[1]+ "" +url + "token=" +gettoken ()); Toast.maketext (Getapplicationcontext (), Strcode, 0). Show (); It.setclass (Getcodeactivity.this, Mainactivity.class); StartActivity (IT); return false; } }); } private String GetToken () {//TODO auto-generated method stub Thread t = new Thread (new Runnable () {@ overridepublic void Run () {//TODO auto-generated method stubstring strURL = "Https://openapi.youku.com/v2/oauth2/token" ; HttpResponse HttpResponse = null; HttpPost HttpPost = new HttpPost (strURL); List<namevaluepair> params = new arraylist<namevaluepair> (); Params.add (New Basicnamevaluepair ("client_id", client_id));//You apply for Youku client_id Params.add (The new Basicnamevaluepair ("cl Ient_secret ", Client_secret));//You apply for Youku Client_secret params.add (" Basicnamevaluepair "," Grant_type _code ")); Params.add (New Basicnamevaluepair ("code", Strcode));//code Params.add just acquired (new Basicnamevaluepair ("Redirec T_uri ", Redirect_url));//callback address. Must be the same as the site try {httppost.setentity (params, HTTP. urlencodedformentity). Utf_8)); LOG.D (TAG, "GetToken () setentity after"); HttpResponse = new Defaulthttpclient (). Execute (httppost); if (Httpresponse.getstatusline (). Getstatuscode () = = () {String result = entityutils.tostring (httprespons E.getentity ()); LOG.D (TAG, "result=" + result); SYSTEM.OUT.PRINTLN (result); Jsonobject object = new Jsonobject (result); Access_token = object.getstring ("Access_token");//access_token get success//toast.ma Ketext (Getapplicationcontext (), "access_token=" +access_token, 0). Show ();//return access_token; }else{log.d (TAG, "GetToken () getstatuscode=" + httpresponse.getstatusline (). Getstatuscode ()); }} catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); LOG.D (TAG, "exception=" + e.tostring ()); }}}); T.start (); try {t.join ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} LOG.D (TAG, "access_token=" +access_token); return access_token; } @Override protected void OnDestroy () {//TODO auto-generated Method stub if (WebView! = null) {WebView = Null } Super.ondestroy (); } }
with this approach, we are able to get the Access_token needed to upload YouTube videos very easily. With Access_token, we were able to set the Access_token to the params parameter so we could upload the video to Youku. For example, the following:
Mainactivity.java
Package Sample;import Java.io.file;import Java.util.arraylist;import java.util.hashmap;import java.util.List;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.message.basicnamevaluepair;import Org.apache.http.protocol.http;import Org.apache.http.util.entityutils;import Org.json.JSONObject;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.os.Environment;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.progressbar;import Android.widget.textview;import Android.widget.toast;import Com.youku.uploader.iuploadresponsehandler;import Com.youku.uploader.r;import Com.youku.uploader.YoukuUploader; public class Mainactivity extends Activity implements Onclicklistener {private ProgressBar PROgressbar;private TextView percent;private youkuuploader uploader;private String client_id = "38bae68624af4531"; private string Client_secret = "feb939fd882b449e1f022aa12fb9b06a";p rivate string code;private string Redirect_uri; private string Access_token;private string TAG = "mainactivity"; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Intent in = Getintent (); code = In.getstringextra ("code"); Access_token = In.getstringextra ("Access_token"); LOG.D (TAG, "token =" +access_token);p Rogressbar = (progressBar) Findviewbyid (r.id.progressbar);p ercent = (TextView) Findviewbyid (r.id.percent); uploader = youkuuploader.getinstance (client_id, Client_secret, Getapplicationcontext ()) ; Bindevents ();} private void Bindevents () {Findviewbyid (r.id.upload). Setonclicklistener (this); Findviewbyid (R.id.cancel). Setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.upload:thread TT = new ThreaD (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubhashmap<string, string> params = new Hashmap<string, string> ();p arams.put ("username", "username");p arams.put ("Password", "password");//There is a need to Access_ Token is set to the number of Params.put ("Access_token", Access_token); LOG.D (TAG, "access_token=" +access_token); hashmap<string, string> uploadinfo = new hashmap<string, string> (); Uploadinfo.put ("title", "Small video"); Uploadinfo.put ("tags", "original");//Uploadinfo.put ("file_name", "/mnt/sdcard2/download/dota2.mp4"); Uploadinfo.put (" File_name ", environment.getexternalstoragedirectory () + File.separator +" Sharemv.mp4 "); LOG.D (TAG, "params=" +params+ "Uploadinfo" +uploadinfo+ "file_name" +uploadinfo.get ("file_name"). toString ()); Uploader.upload (params, uploadinfo, new Iuploadresponsehandler () {@Overridepublic void OnStart () {LOG.V (TAG, "OnStart" );p rogressbar.setprogress (0);p Ercent.settext ("in Wait");} @Overridepublic void onsuccess (jsonobject response) {LOG.V (TAG, "onsuccess" +response.tostriNg ()); String respstr = response.tostring (). substring (response.tostring (). Length ()-2); String str = String.Format ("http://v.youku.com/v_show/id_%s.html?from=y1.7-1.2", respstr); LOG.V (TAG, "onsuccess" + "The URI is:" + str); Toast.maketext (Getapplicationcontext (), "response.tostring ()", 0). Show (); @Overridepublic void onprogressupdate (int counter) {LOG.V (TAG, "onprogressupdate" + Counter + ""); Progressbar.setprogress (counter);p Ercent.settext (counter + "%");} @Overridepublic void OnFailure (Jsonobject errorresponse) {log.v (TAG, "onfailure" + errorresponse.tostring ());} @Overridepublic void onfinished () {LOG.V (TAG, "onfinished");p Ercent.settext ("completed");});}); Tt.start (); Break;case r.id.cancel:if (Uploader.cancel ()) {progressbar.setprogress (0);p ercent.settext (""); Break;}}}
Android calls Youku SDK to upload video