Forbidden issue when call UrlShortener() API - google-url-shortener

I try to transform long url to short url and using "shortener" API on Google Apps Script, but still get "Forbidden" message.
I have created "API Key" at google cloud platform, anything else? thanks.
google's sample code link:
https://developers.google.com/apps-script/advanced/url-shortener?hl=vi
function shortenerUrl(){
var url = UrlShortener.Url.insert(
{
longUrl: "http://www.example.com"
});
Logger.log('Shortened URL is "%s".', url);`enter code here`
}

Related

How to POST to NetSuite custom record from external site?

I'm trying to integrate a very small custom web application with NetSuite. I want a custom record to be created in NetSuite whenever a user clicks a button in my web application.
I have written a RESTlet that works with the REST API Testing chrome extension. I have successfully created records through that chrome extension.
However, when I try to POST from my web application, I get this error:
"Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'null' is therefore not allowed access. The response
had HTTP status code 401."
How can I POST to NetSuite with a RESTlet from an external site? Should I even be using a RESTlet or is there a better way?
RESTlets are meant more as a system to system technology. They require authentication and if you are doing that from a public app your credentials will be compromised.
Netsuite doesn't allow you to set a CORS header so your cross domain integration needs to be via a publicly available suitelet and JSONP.
Since JSONP makes use of get requests you need to make sure your url params end up less than about 2k characters. That's not a standard limit so ymmv
patterns I often use:
Client code:
var url = "public suitelet url from deployment screen";
var params = {
mode: 'neworder',
//simple name/value data
};
$.ajax({
url: url+"&"+ $.param(params) +"&jsoncallback=?",
cache:false,
dataType:'json',
success: function(jResp){
if(!jResp.success){
if(jResp.message) alert(jResp.message);
return;
}
// act on the results
}
});
A library function in the suitelet source file.
function _sendJSResponse(request, response, respObject){
response.setContentType('JAVASCRIPT');
//response.setHeader('Access-Control-Allow-Origin', '*');
var callbackFcn = request.getParameter("jsoncallback") || request.getParameter('callback');
if(callbackFcn){
response.writeLine( callbackFcn + "(" + JSON.stringify(respObject) + ");");
}else response.writeLine( JSON.stringify(respObject) );
}
and then a Suitelet function
function service(request, response){
... do some work and generate a response
var returnObj = {
success:true,
message: '',
result:result
};
_sendJSResponse(request, response, returnObj);
}
That's your browser and its CORS setting
If using chrome ( you should be ;) ) on Windows, create a chrome shortcut with the following flags
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir
Then, kill/restart ALL INSTANCES of chrome in the Task Manager and try your requests again
Otherwise google "disable CORS on MY_BROWSER"

OneNote API - 401 Error

I am following the blog (http://blogs.msdn.com/b/onenotedev/archive/2015/06/11/and-sharepoint-makes-three.aspx) to access one note files stored in Office 365 sharepoint tenant. I registered my app and was able to acquire token but when When I use the accessToken access the following URL
www.onenote.com/.../FromUrl(url='https://*****.sharepoint.com/sites/")
I am always getting a 401. Any idea what is going wrong?
This is Sharad from OneNote Team, Microsoft.
We have a sample (native) app published in the GitHub
github.com/OneNoteDev/OneNoteAPISampleWinUniversal
Please take a look at the file -
OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/O365Auth.cs
There is resource Uri and redirect Uri. Redirect Uri is your app controlled, but resource Uri is always the same.
Code (native app):
private const string AuthContextUrl = "https://login.windows.net/Common";
private const string ResourceUri = "https://onenote.com";
// TODO: Replace the below RedirectUri with your app's RedirectUri.
private const string RedirectUri = "https://localhost";
_authenticationResult =
await AuthContext.AcquireTokenAsync(GetResourceHost(ResourceUri), ClientId, new Uri(RedirectUri), PromptBehavior.Always);
Native app - take a look at this blog.
Web app - take a look at this blog
I hope this helps resolve your issue. Feel free to get back with fiddler trace (request/response).
It appears the Authentication header in your API requests is missing the 'Bearer' prefix before the OAuth token. That's why you get a 401-Unauthorized response.
Our Github repo has code samples on O365 auth, it might be a good reference resource.

Facebook api - invalid token

tried to send post to facebook api to scrape my page at load.
http://i.stack.imgur.com/3Jd0y.png
what is wrong with my code?
This is how you use FB.api:
FB.api('/', 'post', {
id: 'url-to-scrape',
scrape: true
}, function (response) {
console.log(response);
});
Not sure where you got that code example from, but you should always take a look at the Facebook docs.
I you are getting invalid token means your token is not correct let me tell you how to generate token it is simply by combining your app id and app secret gotten on Facebook developers app dashboard after you have signed up check here and read url and to check errors faster on your code api, download post man here www.getpostman.com, put in your api call code in the url bar and hit send it will tell you your error. Then to get token for facebook fancount,pageid,likescount, you can do a search for more info on graph.facebook.com on google. contact me if there is problem.
$appid = "xxxxxxxxxxxxxxx";
$appsecret = "yyyyyyyyyyyyyyyyyyyyyyyyy";
//get it from your dashboard
$token = $appid .|. $appsecret key

c# post photo on facebook without using c# sdk

please provide me a simple piece of code in c# to post photo on facebook without using the very famous facebook sdk for c#, as per my knowledge there are two methods of posting photos,
METHOD 1:
The fb documentation below shows a method to post image with the url provided,
https://developers.facebook.com/blog/post/526/?ref=nf
of course I tried, it does not seem to accept my image url, when I tried debugging here on facebook API explorer using the post method and entered the parameters as below,
SomeAlbumID/photos?=access_token=MyTOKEN&url=http%3a%2f%2fcutree.com%2fcutreefbapp%2fimg1.bmp&message=Family+Tree
It returns an exception saying
{
"error": {
"message": "http\u00253a\u00252f\u00252fcutree.com\u00252fcutreefbapp\u00252fimg1.bmp is an internal url, but this is an external request.",
"type": "CurlUrlInvalidException"
}
}
"internal url, but this is an external request." I am not sure what this means as I am using the same domain as registered on my fbapp, and also giving the request from the server itself.
I have read some where that fb accepts images from only a few servers, can anyone help me out.
METHOD 2:
This is a method where image data in bytes are atttached with the Post body as fb says "To publish a photo, issue a POST request with the photo file attachment as multipart/form-data."
However everyone does that using the fb sdk for c#, can anyone provide simple http post method for this issue.
I have tried streaming image data using a method below
public MyFacebookClass FBPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create("https://graph.facebook.com/" + URI);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = BmpToBytes_Serialization(new Bitmap("C:\\Users\\atul\\cutreefbapp\\DefaultThumb.bmp"));
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<MyFacebookClass>(sr.ReadToEnd().Trim());
}
Posting photos by giving a parameter url definitively works. If it doesn’t for you, then you are doing something wrong.
(I just tried it with the URL of your picture on the Graph API Explorer, and it work as I expected it.)
when I tried debugging here on facebook API explorer using the post method and entered the parameters as below,
SomeAlbumID/photos?=access_token=MyTOKEN&url=http%3a%2f%2fcutree.com%2fcutreefbapp%2fimg1.bmp&message=Family+Tree
If that’s the actual address you tried to post to, then the = between photos? and access_token is clearly wrong.
The problem you have is your code isn't correct. In order to post a photo to Facebook you'll need to use a multi-part form data post. I haven't used the C# SDK, but I'm sure it builds a multi-part form post internally before submitting the image.
You will have to do something similar to what is posted here. I was about to post my code that does this exactly for Facebook from my app, but it is a bit long.

Titanium.Facebook getting graph api access token

I'm developing an iPhone app with Appcelerator Titanium SDK 1.6.2
I am upload an photo to a users facebook album with the Titanium Facebook module, graph api.
The upload goes just fine and returns the items unique ID.
When I try to parse the JSON from the unique id I'm told i need to pass an access token, which makes sense.
How do I get the access token to be passed to the graph request url?
When I do a XHR GET request while passing Ti.Facebook.accessToken I get the following error
URL: https://graph.facebook.com/10150527948301195?access_token=t4CqzHallahfy4d7RnERrJb4ffOkQfJvrYrGEBoZ4so.epdiI6IjJGR2ZFY1ZTMHh6RlR6ZmNIcVctMHcifQ.NBRMth0vb7pXKcd8lHNz9aremoyNpvrbhz2P3zkgWJU4eHdfewOp1WruBNZS_lSDy0XM0Xu0ACry8aEmSckGJVQJxEioykrNZhT7S9mJG2OKWqMdk6ucg5IMhXMfndF9sdKwWrWb7uPKI57LzIOf5lvA
{
error = {
message = "Unsupported post request.";
type = GraphMethodException;
};
}
And if I don't pass Ti.Facebook.accessToken I'm asked for it.
I'm bound to be missing something, any help would be greatly appreciated.
see this link http://developer.appcelerator.com/blog/2011/02/facebook-module-changes-in-titanium-mobile-1-6-0.html
I think you should be using Titanium.Facebook.requestWithGraphPath(...)