Google API and Oauth2

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Google API and Oauth2

Post by sefrojones » Sat Jul 26, 2014 8:37 pm

I would like to use google's API to connect a LiveCode app to a blogger account. Basically I have a couple thousand articles that need to be posted to a blogger page. The app would go through a folder of TXT files and parse them into title and content sections, then use the blogger API blogger.posts.insert to post them to a blog. I am certain LiveCode can do it, but I am a little lost on how to connect my application to the account using the Oath2 ClientID and secret. My google-fu is coming up short, so if anyone could point me in the direction of a good tutorial,library, or anything really,it would be much appreciated!

Thanks in advance,

--Sefro

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Google API and Oauth2

Post by sefrojones » Sun Jul 27, 2014 3:38 pm

A little more info:

I am able to get an authorization token by launching a URL that looks something like this:

Code: Select all

https://accounts.google.com/o/oauth2/auth?redirect_uri="&tRedirect_URI&"&response_type=code&client_id="  \
       &tClientID&"&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2F"&tscope&"&approval_prompt=force&access_type=online
Then I set the httpheaders to this:

Code: Select all

POST https://www.googleapis.com/blogger/v3/blogs/[myBlogID)/posts/
Authorization: [The authorization Token I just recieved]
Content-Type: application/json

finally i try to urlencode the JSON and POST it to the API:

Code: Select all

{
  "kind": "blogger#post",
  "blog": {
    "id": "[myBlogID]"
  },
  "title": "This is a Test Article",
  "content": "This is the content of my Article!"
}
When I try to post, it throws an error 411 Length Required...... Any hints? I am trying to use the guide from here:
https://developers.google.com/blogger/d ... ddingAPost to get this working. I would love any ideas on how to get this to work.....

--sefro

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Google API and Oauth2

Post by sefrojones » Sun Jul 27, 2014 9:12 pm

A glimmer of hope from an old link:

http://runtime-revolution.278305.n4.nab ... 45121.html

But upon further googling I can't find anything about lib.aag.google, so I'm not sure if it ever happened....

I really not sure what to try.... Anybody?
I7Yl0L5.jpg

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Google API and Oauth2

Post by Simon » Sun Jul 27, 2014 10:45 pm

Hi Sefro,
Not sure how much this will help but I think it's asking for "Content-Length:" like "Content-Type: application/json"
Check out this script from Jacque for use:

Code: Select all

on startup
  put "tipTemplate.txt" into theTemplateFile
  if there is not a file theTemplateFile then
    put "Error: template missing" into theData
  else
    put url ("file:"&theTemplateFile) into theData
    put offset("[*]",theData) into theStartChar
    put getTip() into char theStartChar to theStartChar+2 of theData
  end if
  
  # write minimal set of HTTP headers to stdout
  put "Content-Type: text/html" & cr
  put "Content-Length:" && the length of theData & cr & cr --"length" is a liveCode function
  put theData
end startup
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Google API and Oauth2

Post by sefrojones » Sun Jul 27, 2014 11:49 pm

Thanks Simon,

I changed my header to look like this:


POST https://www.googleapis.com/blogger/v3/blogs/[myBlogID)/posts/
Authorization: [The authorization Token I just recieved]
Content-Type: application/json
Content-Length: 162


162 is the length of the sample JSON I am trying to post, still getting an error 411. It is quite possible that I am missing something, but not sure what.......

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Google API and Oauth2

Post by sefrojones » Wed Jul 30, 2014 5:36 pm

I still haven't figured this one out. This is the script that I am trying to use to post:

Code: Select all

on mouseup
   put fld "blogID" into tBlogID
   put field "token" into tToken
   put field "Title" into tTitle
  put field "body" into tBody
   set the httpHeaders to \
         "Authorization:"&&tToken & cr & \
          "Content-Type: application/json"
   put "{"&quote&"kind"&quote&":"&&quote & "blogger#post"  &quote& ","\
   &quote&"blog"&quote&":"& "{" &quote &"id"& quote & ":" &quote &tBlogID&quote&"},"\
   &quote&"title"&quote&":"&quote& tTitle&quote&","\
   &quote&"content"& quote & ":" & quote &tBody& quote&"}" into tPost
   put "https://www.googleapis.com/blogger/v3/blogs/"&tBlogID&"/posts/" into tBlogURL
   post tPost to url  tBlogURL
   answer the result
   answer tblogURL
   
end mouseup
Does anybody see anything I am missing here?

--Sefro

Post Reply