Could not authenticate you - twitter api v1.1 - javascript

I am trying to use the Twitter API to tweet, retweet, and upload images, while tweet works as intended, upload media don't work. more information below
packages i use :
node-fetch
deepmerge - to merge the given options
oauth-1.0a
crypto
the error appears at all the fetch request related to this function
Error:
{ errors: [ { code: 32, message: 'Could not authenticate you.' } ] }
I know whats the error is about, just asking this as I am confused why this would appear only in this function,as the auth info is correct and the same function used to create the headers work in another function
some important functions and related : https://hastebin.com/eyepikidax.kotlin
the code affected : https://hastebin.com/inehomodag.js
tweet function : (the working function) : https://hastebin.com/xatebakahe.properties
even though the tweet works with the same function _makeRequest(custom function) uploadmedia dont
uploadmedia is same as tweet but with extra steps like uploading media
i logged the headers provided by _makeRequest for the tweet function and the upload function
headers: https://hastebin.com/xojotupine.rust
it seems in the uploadmedia function the headers became invalid but in the tweet function they are valid

The issue was with the authorization headers, I had to generate them for each request.It was easy as creating a wrapper function that will take the request details (the needed ones) and output the headers
I have a Twitter API library written in typescript (unmaintained for now :/ )
That uses this, don't suggest completely using that, just take what you need from it
Code Related
^^
Above Links to - https://github.com/typicalninja493/tweets.ts/blob/master/src/rest/bucket.ts#L200

Related

WP REST API ERROR 401 While trying to fetch image from wp:featuredmedia

I'm trying to fetch featured image from my published post but it seems impossible! I get
an error :( that's the code:
function fetchSlideShow(){
let endpoint = "http://loreleiheckmann.com/wordpress/wordpress/wp-json/wp/v2/Vinyls?_embed";
fetch(endpoint)
.then(e => e.json())
.then(showSlideShow);
}
function showSlideShow(data){
console.log(data);
data.forEach(showSingleSlide);
showSlides();
}
function showSingleSlide(aSlide) {
let template = document.querySelector(".slide_template").content;
let clone = template.cloneNode(true);
console.log(aSlide);
clone.querySelector("img").setAttribute("src", aSlide._embedded["wp:featuredmedia"]
[0].media_details.source_url);
let SlideList = document.querySelector("#SlideList");
SlideList.appendChild(clone);
}
While going to the array I see error 401 :( and moreover: Cannot read property 'source_url' of undefined" I don't know what I'm doing wrong .. any insights?
HERE ARE THE ERRORS -> 401 ON CONSOLE + PROBLEM WITH URL.:
Please try changing your url in the endpoint variable:
let endpoint = "http://loreleiheckmann.com/wordpress/wordpress/wp-json/wp/v2/posts?per_page=20&_embed=wp:featuredmedia
If you need more data you can add them with a comma:
wp-json/wp/v2/posts?per_page=20&_embed=wp:term,wp:featuredmedia
Post per page is optional, but I would prefer having that set.
And you should write what image size you need. That way rest api will give you the right source url:
_embedded['wp:featuredmedia']['0'].media_details.sizes.medium_large.source_url
I think it would also be better practice if you use an ssl certificate, there are free by "Let's encrypt".
EDIT:
Your screenshot shows the rest api message:
wp:featuredmedia: Array(1)
0:
code: "rest_forbidden"
data: {status: 401}
message: "Sorry, you are not allowed to do that."
So the 401 status code means: unauthorised
Seems like you are not allowed to access the data via rest api.
There could be multiple reasons for that:
The image is attached to an unpublished post. The post may have the status "private" or "draft", and therefore is not public available.
Your post (and attached image) is not available for not logged in users. If there is a restriction to view your code, this also applies to rest api.
Maybe you are using some kind of membership plugin that restricts the wp rest-api. Try deactivating all plugins if one of them affects the behaviour.
You have added some custom code to restrict access rest api access.
If nothing works for you, you should look into your database and check the medias post_status.
I think it is working fine, but you do not have access to view the posts data. This is what the 401 error indicates.
To whom it may concern - In the end, I added image in WP by custom post type ( I gave up wp:featured media - I wasn't able to fetch it.) Afterward I added a code to JS -> b.querySelector(".img").setAttribute("src", a.acf.image.url); so it works:)

RingCentral JavaScript SDK Handle Redirect URI points to local JavaScript Function

RingCentral JavaScript SDK handle redirect URI point to local JavaScript function
As-per Doc, they give option
RingCentral.SDK.handleLoginRedirect()
but dont know how to use that
var SDK = require('ringcentral');
rcsdk = new SDK({
server: SDK.server.sandbox,
appKey: '_app_key',
appSecret: 'app_password',
redirectUri: ''
})
function handleredirectURI(){
//handle redirections
}
We need points out our handleredirectURI function
Thanks in advance
Per the documentation. The context is 3-legged oAuth. Here is a demo: https://github.com/ringcentral/ringcentral-demos-oauth/tree/master/javascript
However the demo doesn't use the handleredirectURI method. Which means that the method is simply an utility method and it's not required to use it.
For the usage of handleredirectURI, I will come back and update my answer later.
Update
Here is the source code for handleredirectURI: https://github.com/ringcentral/ringcentral-js/blob/669b7d06254d3620c5a5f24c94b401aa862be948/src/SDK.js#L115-L124
You can see that the method parses win.location to get some useful data and postMessage back to its opener.
Unit tests for handleredirectURI: https://github.com/ringcentral/ringcentral-js/blob/669b7d06254d3620c5a5f24c94b401aa862be948/src/SDK-spec.js#L27-L63
Update 2
I read handleredirectURI' source code, unit tests, sample code again and I think its usage is just like what is written in its documentation:
Popup Setup
This setup is good when your app is rendered as a widget on a third-party sites.
If you would like to simply open RingCentral login pages in a popup, you may use the following short-hand in your app's login page:
var platform = rcsdk.platform();
var loginUrl = platform.loginUrl({implicit: true}); // implicit parameter is optional, default false
platform
.loginWindow({url: loginUrl}) // this method also allows to supply more options to control window position
.then(function (loginOptions){
return platform.login(loginOptions);
})
.then(...)
.catch(...);
In this case your landing page (the one to which Redirect URI points) need to call the following code:
RingCentral.SDK.handleLoginRedirect();
Explained:
Run the first code snippet to open the login popup.
In the redirected to page, run the second code snippet (that one line of code) to get everything else done.
After that the authorization part is done and you can invoke other APIs.
Let me know if you have more questions.

Firebase cloud notification localization

Trying to get localization keys to work with firebase and nodes firebase.admin utility but cannot get it to work.
I receive a messageId when sending and no errors at all so a little stuck.
I use a Firebase Function to send notifications so everything is running within firebase.
admin.messaging().sendToTopic("my_topic", {
"notification": {
"titleLocKey" : "FRXT",
"bodyLocKey" : "FRXB",
"bodyLocArgs" : "['test']"
}
}).then(function(resp){
res.send(200, resp)
})
I guess i'm doing something wrong here that i just cannot see so if there are any smart people out there please give a shout. The keys used are in the iOS localization string file. If i just use title and body the push work fine also.

Chromium error with SPDY when using Facebook JavaScript SDK

I develop a mobile application with HTML5 + Cordova (Phonegap) using Facebook Javascript SDK for iOs and Android.
I implement it like this :
FB.api(
{
method: 'fql.query',
query: 'SELECT uid, name, birthday_date FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'
},
function(response) {
console.log(response);
// some code
});
It worked for several months on Android & iOs, until yesterday, on Android.
An error occurs when the call to the api is doing and the callback function is not called.
Here is the error from LogCat in eclipse :
02-26 12:27:10.526: W/chromium(22379):
external/chromium/net/spdy/spdy_session.cc:1246:
[0226/122710:WARNING:spdy_session.cc(1246)] Could not parse Spdy
Control Frame Header. 02-26 12:27:10.526: D/chromium(22379): Unknown
chromium error: -337 02-26 12:27:10.526: W/chromium(22379):
external/chromium/net/spdy/spdy_session.cc:1058:
[0226/122710:WARNING:spdy_session.cc(1058)] Received data frame for
invalid stream 1 02-26 12:27:10.526: W/chromium(22379):
external/chromium/net/spdy/spdy_session.cc:1058:
[0226/122710:WARNING:spdy_session.cc(1058)] Received data frame for
invalid stream 1
It seems to be an error of the android browser Chromium when calling query Facebook (which using the protocol spdy)
Anyone has an idea ?
Thank you !
Seeing the same thing!
/chromium( 1942): external/chromium/net/spdy/spdy_session.cc:1246: [0307/015242:WARNING:spdy_session.cc(1246)] Could not parse Spdy Control Frame Header.
W/chromium( 1942): external/chromium/net/spdy/spdy_session.cc:1058: [0307/015242:WARNING:spdy_session.cc(1058)] Received data frame for invalid stream 21
W/chromium( 1942): external/chromium/net/spdy/spdy_session.cc:1058: [0307/015242:WARNING:spdy_session.cc(1058)] Received data frame for invalid stream 21
D/chromium( 1942): Unknown chromium error: -337
Found news about FB implementing SPDY without much news:
http://zoompf.com/2013/03/facebook-adds-spdy-support
Surprised we're not seeing more issues around.
I have the same problem with the similar code under identical
circumstances, PhoneGap, Android
The solution is to switch to Graph
API. Using Graphi API the same call will be
FB.api('/me/friends', { fields: 'id, name, gender, username' }, function (response) { ... });
There are a number of caveats that might
not be applicable to you but were applicable to me
The fields named differently. “sex” is now “gender”, for example Graph API does not
have direct analog of is_app_user field. If you add it to the list
of fields, you’ll get an error. There is a workaround asking
specifically for the users who installed the app
There is paging
implemented in response. So the response is structured a bit
differently. If you used to pass the data for processing to
functions you shall pass not “response” but “response.data”, and
watch for “response.paging”, to see if there are more friends to add
by additional calls.

JavaScript OAuth sign in with Twitter

I have to integrate Sign-in-with Twitter in my app as below.
https://dev.twitter.com/docs/auth/sign-twitter
It is browser based app developed in JavaScript
I have been refering google code java script OAuth, but im confused how to use oauth/authenticate and how to get the oauth_token
Can any one please help me out with some samples ?
Problem with this is that anyone who views your page can also now view your consumer key and secret which must be kept private.
Now, someone could write an app that uses your app credentials and do naughty and bad things to the point that twitter and users ban you, and there isnt anything you can do.
Twitter do state that every possible effort must be made to keep these values private to avoid this happening
Unfortunately, there is currently no way to use oAuth in Browser based JavaScript securely.
Check out the Twitter #Anywhere JSDK authComplete and signOut events and callbacks at https://dev.twitter.com/docs/anywhere/welcome#login-signup
twttr.anywhere(function (T) {
T.bind("authComplete", function (e, user) {
// triggered when auth completed successfully
});
T.bind("signOut", function (e) {
// triggered when user logs out
});
});
Use below code in consumer.js and select example provider in index.html drop down list
consumer.example =
{ consumerKey : "your_app_key"
, consumerSecret: "your_app_secret"
, serviceProvider:
{ signatureMethod : "HMAC-SHA1"
, requestTokenURL : "https://twitter.com/oauth/request_token"
, userAuthorizationURL: "https://twitter.com/oauth/authorize"
, accessTokenURL : "https://twitter.com/oauth/access_token"
, echoURL : "http://localhost/oauth-provider/echo"
}
};
Since I was searching for the same thing, trying not to use a custom PHP solution, I came across this very simple integration at http://www.fullondesign.co.uk/coding/2516-how-use-twitter-oauth-1-1-javascriptjquery.htm which uses a PHP proxy that accepts whatever twitter api call you'd like to retrieve from your javascript ..
I'm definitely taking a look at it

Categories