Add New Gist using the Github API - javascript

I'm making a small app in Adobe Air which I need to interact with the Github Gist API. However I'm kind of stuck.
If you're not familiar with Adobe Air you can still help, the XMLHttpRequest javascript object can do cross domain requests, as there is no domain as such. So theres nothing Adobe Air specific here.
Where I'm stuck is I think I need to authenticate myself then make the POST. I just don't understand it

The problem with your script is that though you're sending a POST method, you're adding the data in the URL as though it were a GET. You just need to change xmlhttp.send(NULL) to xmlhttp.send(data), where data is the query data you were appending to the gists URL before (including the file and authentication information).
As a simple example, here's an excerpt from a bash script creating a new gist:
#!/usr/bin/env bash
if [ -z "$(git config github.token)" ]
then echo "warning: no api key found, to add follow instructions on github account page."
else echo "attempting to create a new gist using your github authentication..."; fi
SHA=$((curl https://gist.github.com/gists --include \
--data login=$(git config github.user) \
--data token=$(git config github.token) \
--data action_button=private \
--data 'file_ext[gistfile1]=txt' \
--data 'file_contents[gistfile1]=Hello World, this is an example gist!' \
| perl -e 'for(<>){if(/^Location: https?:\/\/gist.github.com\/([0-9a-f]+)/){print $1}}')2>/dev/null)
echo "New example gist created at https://gist.github.com/$SHA"

You shouldn't have to authenticate the user.
The XMLHttpRequest will just have to have the user name and the users API token included in the request.
Looking at the example ruby script provided by github here, you just have to provide the following attributes:
File Extenstion
file name
content
If the gist is private or not
User login
User API Token
python and perl versions of the script

Related

Can curl get audio files from BLOBs?

I'm trying to use curl to download the wav file (or at least get a URL to point a downloader at) that is generated when you go to https://tetyys.com/SAPI4, enter text in the edit field, click "Say it" and click "Play".
If I view the page source, or right-click on the play button and click "Copy audio location", it shows that the source of the wave file is a BLOB (i.e. blob:https://tetyys.com/someHexNumber).
By viewing the script at https://tetyys.com/SAPI4/scripts/tts.js, specifically the string var "url", I determined that you can set all the values in the url like so:
https://tetyys.com/SAPI4/SAPI4?text=whatever+you+want+it+to+say&voice=Sam&pitch=100&speed=100
When I type that into Firefox, I get a dialog asking if I want to open the wav file, and when I open it, it plays as expected.
So, is there a way to do that with curl (or otherwise seamlessly from the command line) instead of Firefox?
Downloads with curl can be achieved via option -o or --output <file>, whereby -O would try to use the --remote-name. Please see man curl for more details.
You should try with a structure like
curl -o /opt/curl-7.65.3.tar.gz https://curl.haxx.se/download/curl-7.65.3.tar.gz
I also assume your specific case might need more work and a script. Because the call
curl -o test.wav https://tetyys.com/SAPI4/SAPI4 text=test&voice=Sam&pitch=100&speed=100
is generating another source. If you check the response and the header from above call you will find
Request URL:blob:https://tetyys.com/<UUID>
Request Method:GET
Status Code:206 Partial Content (from disk cache)
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Content-Length:40166
Content-Range:bytes 0-40165/40166
Content-Type:audio/wav
so to download the audio file you would need something like
curl -o test.wav https://tetyys.com/${UUID}
maybe together with necessary cookies and headers, at least -H "Keep-Alive: 60" -H "Connection: keep-alive" to keep the session open.

Cannot publish content from site to Contentful -- ValidationFailed

Contentful newbie here. I'm having an issue with publishing content from our team website to Contentful using Contentful's API (C# backend).
Background: We have a content type called Post with a required field called Tags. Whenever I try to publish content from our testing website to our Contentful space, I get the following error:
Here's the data I've been trying to pass. As you can see, tags is not empty:
"{"entry":{
"fields":{
"title":{"en-US":"test post 4"},
"description":{"en-US":"test comment 4"},
"tags":{"en-US":["Lobsters; test data","United States of America","China"]},
"author":{"en-US":"admin"},
"authorId":{"en-US":6},
"yearStart":{"en-US":"2003"},
"yearEnd":{"en-US":"2015"}
}
}
}"
I can't replicate this issue in my local environment. Tried contacting Contentful Support as well and they are as clueless about the cause as I am.
Any idea what could be causing this error?
Thank you.
from the validation errors' detail field, it looks like you're using the /spaces/:spaceId/assets endpoint, not the /entries one, which is probably the one you want to use.
url is a special field for assets, which indicates where the actual file is located the Asset is referencing.
So make sure you're doing something like this:
curl \
-X PUT \
-H "Authorization: Bearer <accessToken>" \
-H "X-Contentful-Content-Type: post" \
-d '{"entry":{"fields":{"title":{"en-US":"test post 4"},"description":{"en-US":"test comment 4"},"tags":{"en-US":["Lobsters; test data","United States of America","China"]},"author":{"en-US":"admin"},"authorId":{"en-US":6},"yearStart":{"en-US":"2003"},"yearEnd":{"en-US":"2015"}}}}' \
https://api.contentful.com/spaces/<spaceId>/entries

Understanding ms-seo for meteor

I'm trying to use ms-seo package for meteor but I'm not understanding how it works.
It's supposed to add meta tags to your page for crawlers and social media (google, facebook, twitter, etc...)
To see it working according to the docs all I should have to do is
meteor add manuelschoebel:ms-seo
and then add some defaults
Meteor.startup(function () {
if(Meteor.isClient){
return SEO.config({
title: 'Manuel Schoebel - MVP Development',
meta: {
'description': 'Manuel Schoebel develops Minimal Viable Producs (MVP) for Startups',
},
og: {
'image': 'http://manuel-schoebel.com/images/authors/manuel-schoebel.jpg',
}
});
}
});
which I did but that code only executes on the client (browser). How is that helpful to search engines?
So I test it
curl http://localhost:3000
Results have no tags
If In the browser I go to http://localhost:3000 and inspect the elements in the debugger I see the tag but if I check the source I don't.
I don't understand how client side added tags have anything to do with SEO. I thought Google, Facebook, Twitter when scanning your page for meta tags basically just do a single request. Effectively the same as curl http://localhost:3000
So how does this package actually do anything useful? I feel stupid. 27k users it must work but I don't understand how. Does it require the spiderable package to get static pages generated?
You are correct. You need to use something like the spiderable package or prerender.io to get this to work. This package will add tags, but like any Meteor page, it's rendered on the client.
Try this with curl to see the result when using spiderable:
curl http://localhost:3000/?_escaped_fragment_=
Google will now render the JS itself so for Google to index your page correctly you don't need to use spiderable/prerender.io, but for other search engines I believe you still do have to.
An alternate answer:
Don't use spiderable, as it uses PhantomJS which is rather resource intensive when bots crawl your site.
Many Meteor devs are using Prerender these days, check it out.
If you still have some problems with social share buttons or the package, try to read this: https://webdevelopment7636.wordpress.com/2017/02/15/social-share-with-meteor/ . It was the only way I got mine to work. You don't have to worry about phantomJS or spiderable to make it work fine.
It is a complete tutorial using meteorhacks:ssr and meteorhacks:picker. You have to create a crawler filter on the server side and a route that will be called by it when it is activated. The route will send dynamically the template and the data to a html on the "private" folder, and will render the html to the crawler. The template on the private folder will be the that gets the metatags and the tag.
This is the file that will be on the private folder
I can't put the other links with the code here, but if you need anymore help, go to the first link and see if the tutorial helps.

What does "curl" mean?

I'm developing Facebook JavaScript apps on a daily basis, but keep stumbling into some code snippets I don't understand, on the Facebook Documentation, and other websites I visit.
I searched Google for CURL, and found some descriptions about it. I can't figure out how Facebook wants me to use it.
curl -F "title=Example Title" -F "description=Description" \
-F "start_time=1329417443" \
"https://graph.facebook.com/PAGE_ID/milestones?access_token=_"
It's nonsens for me. Can you help me understand in what context I can use it for Facebook , and maybe in general, and guide me in the right direction where to find more on the subject?
curl is a command line utility that lets you send an HTTP request. It can be very useful for developing with web service APIs. I believe it comes pre-installed with most linux distros but you would need to download and install it for Windows. (It probably comes with Cygwin but can be installed on its own as well.)
I would suggest making sure it's directory is added to your PATH environmental variables. Again, probably not a problem in linux but you will need to do this manually in windows.
curl is a command to fetch requests. The -F (--form) argument is used to specify form POST parameters.
Citation from man curl:
-F/--form <name=content>
(HTTP) This lets curl emulate a filled-in form in which a user
has pressed the submit button. This causes curl to POST data
using the Content-Type multipart/form-data according to RFC
2388. This enables uploading of binary files etc. To force the
'content' part to be a file, prefix the file name with an #
sign. To just get the content part from a file, prefix the file
name with the symbol <. The difference between # and < is then
that # makes a file get attached in the post as a file upload,
while the < makes a text field and just get the contents for
that text field from a file.
curl is a way of fetching items. The -F is one of many parameters...
http://curl.haxx.se/docs/manpage.html
Also:
Have you seen http://developers.facebook.com/docs/reference/api/batch/
and it could be useful for something like:
http://chaolam.wordpress.com/2010/06/07/implementing-facebook-real-time-updates-api-with-curl-examples/
of course FB docs use curl to show a common basic way to perform the request ... it depends on what platform language libraries are you using the actual way to perform the graph http request
...so that if you are Facebook JavaScript developer you have to use XMLHttpRequest (or i suppose facebook js lib calls)

Use CGI and Javascript to save a file

I have some JSON scripts that I plan on parsing on the site and then allowing the clients to edit them through my interface (to later be loaded and parsed once again for display). Problem is, Javascript doesn't have access to writing to the file system. I've already got a system for reading the JSON files using Javascript (and jQuery). Now, I've heard I can use CGI to save the data later. Can somebody give me some references and in depth explanations? I've read a bit about CGI in general but nothing specific.
Thanks for any help!
CGI is a way for servers to interface with scripts. Pretty much, you just set up the server to execute a file, and it will execute it with several environment variables set and POST data fed to its standard input. The script should output the headers for the page, followed by the content.
CGI scripts can be written in many different languages. Perl is well-known for CGI scripts; it has documentation on it here. Python has a cgi module to deal with CGI. Ruby has a CGI package as well.
Here's a quick CGI script written in Python that writes to a file. You'll probably want to modify it or use it as a reference rather than using it as-is:
#!/usr/bin/env python
import os
import os.path
import sys
import json
import cgi
# You'll probably want to remove or alter
# the following line for production.
import cgitb; cgitb.enable()
def bad_request():
print "Status: 400 Bad Request"
print "Content-Type: application/json"
print ""
json.dump({'success': False}, sys.stdout)
sys.exit(0)
assert 'REQUEST_METHOD' in os.environ
if os.environ['REQUEST_METHOD'] != 'POST':
bad_request()
form = cgi.FieldStorage()
if 'data' not in form:
bad_request()
filename = os.path.join(os.path.dirname(__file__), "some_file.json")
with open(filename, "wb") as f:
f.write(form['data'].value)
print "Content-Type: application/json"
print ""
json.dump({'success': True}, sys.stdout)
If you POST to it with a data parameter, it will save that data into some_file.json in the same directory as itself.

Categories

Resources