2

I am trying to post photos to google plus via HTTP API(No SDK used) from my webpage.

Here is my code

<html>
<head>
<script></script>
</head>
<body>
<script>

function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();

  if ("withCredentials" in xhr) {    // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {    // CORS not supported.
    xhr = null;

  }
  return xhr;
}


function PostImageToGoogle()
{
    var options = {
   "mediaUrl": "https://lh5.googleusercontent.com/-lQJ323klqg4/AAAAAAAAAAI/AAAAAAAABuw/DiqAtVEMftk/photo.jpg"
    };

    var x = JSON.stringify(options);

    var xhr = new XMLHttpRequest();

    var gurl = 'https://www.googleapis.com/plusDomains/v1/people/me/media/cloud?access_token=ya29.HQCBp_1_cLr65kgAAAAplbpjbqQhzqWY_tRYprR8O0QJrsIQ93OOJuPmUpE8kRsB8-rzBPeGyjvXtFlia92OoHdoxo8NH7uJRllianuAOju_ayLqrrmGIkUMRlGhGg&key=1364677236854-bc4lhgs7lkjoaohreihbssk8gnims1uf.apps.googleusercontent.com' ;   

   xhr.open( 'POST', gurl,true );

   xhr.onload = function() {
        alert( "success" + xhr.responseText );
    };
    xhr.onerror = function() {
    alert("failed" + xhr.responseText);
    };
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(x);
} 

</script>
<div><label width="80%">Hello! WelCome </label></div>
<form>
<a href="#" onclick="PostImageToGoogle();">post photo</a>
</form>
</body>
</html>

Access_token is generated using the scopes --> plus.me , stream.write, Media.uplode API Enabled are --> Google+ , Google+ Domain

When ever I make request i am getting an error in successcallback as forbidden

What is the best approach to share photos on google plus stream without using any SDK. Any sample links ?

Thanks

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Karthik Dheeraj
  • 1,039
  • 1
  • 13
  • 25
  • How exactly are you getting the AccessToken? – Linda Lawton - DaImTo May 21 '14 at 13:16
  • During my login i am getting the access token to my redirect url page.. from there am taking the access token and making rest call to share photos and comments as mentioned above. You are talking about google Analytic s API. Do I really need it ? – Karthik Dheeraj May 21 '14 at 13:19
  • you need to upload the binary data of the picture not a link to the picture. mediaUrl is the Json response you get back after you have unloaded it. https://developers.google.com/+/domains/posts/attaching-media look under Protocol that's the best link I can give you. – Linda Lawton - DaImTo May 21 '14 at 13:31
  • Can you please guide me to How to do that ? here is the link I follow https://developers.google.com/+/domains/api/media/insert – Karthik Dheeraj May 21 '14 at 13:37
  • I cant help much I have never tried fileupload with JavaScript. Google it found this https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-18/file-upload-with-an-http-post – Linda Lawton - DaImTo May 21 '14 at 13:44

0 Answers0