YouTube Data API: add a subscription -


i'm using youtube's v3 data api add subscription channel. occurs on wordpress installation.

i added google apis (for oauth) on wordpress theme functions:

wp_enqueue_script( 'googleapi', 'https://apis.google.com/js/client.js?onload=googleapiclientready', array(), '1.0.0', true ); 

i added in same way oauth javascript file, first 1 here: https://developers.google.com/youtube/v3/code_samples/javascript.

following guide(https://developers.google.com/youtube/v3/docs/subscriptions/insert (apps script)), extended oauth js addsubscription method.

google client api seems loaded , working calls correctly googleapiclientready on oauth javascript.

so, how subscription being inserted:

oauth javascript

... ... ...  // after api loads function handleapiloaded() {   addsubscription(); } function addsubscription() {   // replace channel id channel id want subscribe   var channelid = 'this filled channel id';   var resource = {     snippet: {       resourceid: {         kind: 'youtube#channel',         channelid: channelid       }     }   };    try {     var response = youtube.subscriptions.insert(resource, 'snippet');     jquery('#success').show();    } catch (e) {     if(e.message.match('subscriptionduplicate')) {       jquery('#success').show();     } else {       jquery('#fail').show();        alert("please send mail () following: error: " + e.message);     }    } 

so, first error comes with

youtube.subscriptions.insert(resource, 'snippet') 

it says youtube not defined. replaced with:

gapi.client.youtube.subscriptions.insert(resource, 'snippet'); 

and error went away. when checking response, subscription isn't completed, get

{"wc":1,"hg":{"ph":null,"hg":{"path":"/youtube/v3/subscriptions","method":"post","params":{},"headers":{},"body":"snippet","root":"https://www.googleapis.com"},"wc":"auto"}} 

so, know what's happening on post request , what's solution this.

i can post full oauth file, it's in example, plus addsubscription method @ end.

okay, got working, problem on post request. here full method working:

// subscribes authorized user channel specified function addsubscription(channelsub) {     var resource = {         part: 'id,snippet',         snippet: {             resourceid: {                 kind: 'youtube#channel',                 channelid: channelsub             }         }     };      var request = gapi.client.youtube.subscriptions.insert(resource);     request.execute(function (response) {         var result = response.result;         if (result) {             // alert("subscription completed");             }         } else {             // alert("subscripion failed");             // ...         }     }); } 

also make sure load google apps api (in fact without authorize/login button won't work) , jquery.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -