Cannot retrieving the expanded_url - twitteroauth / twitter api - twitter

Cannot get the expanded_url from this tweet -> https://twitter.com/i/web/status/1297715123360915457,
but it works fine here -> https://twitter.com/i/web/status/1298033841374822400
I'm using abraham/twitteroauth version 1.2.0, also tried on 1.1.0 and 2.0.1.
Still not retrieving the expanded_url.
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(
TW_KEY,
TW_SECRET,
TW_TOKEN,
TW_TOKEN_SECRET
);
$tweetResult = $connection->get(
"statuses/show",
[
'id' => '1298033841374822400', "tweet_mode" => "extended"
//'id' => '1297715123360915457', "tweet_mode" => "extended"
]
);
var_dump($tweetResult->entities->urls);
result for 1298033841374822400:
array(1) { [0]=> object(stdClass)#11 (4) { ["url"]=> string(23) "the short URL" ["expanded_url"]=> string(53) "https://contest.mdpr.jp/mrcon202003/gal/taiki06191148" ["display_url"]=> string(33) "contest.mdpr.jp/mrcon202003/ga…" ["indices"]=> array(2) { [0]=> int(67) [1]=> int(90) } } }
and result for 1297715123360915457
array(0) { }

The second Tweet is a retweet, so the urls are at
$tweetResult->retweeted_status->entities->urls

Related

Apache Superset and Auth0 returns "unexpected keyword argument 'scope'"

Some time ago I've successfully integrated Superset authentication with Oauth using AWS Cognito.
Now I'm trying to do the same with Auth0, reusing the previous configuration and changing the endpoints according to Auth0 documentation.
Unfortunately, the login fails and Superset's log returns the following message:
2021-10-20 10:30:48,886:ERROR:flask_appbuilder.security.views:Error on OAuth authorize: request() got an unexpected keyword argument 'scope'
This is the Oauth configuration in superset_config.py:
from superset.security import SupersetSecurityManager
import json
import logging
logger = logging.getLogger(__name__)
class CustomSsoSecurityManager(SupersetSecurityManager):
def oauth_user_info(self, provider, response=None):
if provider == 'auth0':
res = self.appbuilder.sm.oauth_remotes[provider].get('userinfo')
if res.raw.status != 200:
logger.error('Failed to obtain user info: %s', res.data)
return
me = json.loads(res._content)
logger.warning(" user_data: %s", me)
prefix = 'Superset'
logging.warning("user_data: {0}".format(me))
return {
'username' : me['email'],
'name' : me['name'],
'email' : me['email'],
'first_name': me['given_name'],
'last_name': me['family_name'],
}
AUTH_TYPE = AUTH_OAUTH
AUTH_USER_REGISTRATION = True
AUTH_USER_REGISTRATION_ROLE = "Public"
AUTH0_URL = os.getenv('AUTH0_URL')
AUTH0_CLIENT_KEY = os.getenv('AUTH0_CLIENT_KEY')
AUTH0_CLIENT_SECRET = os.getenv('AUTH0_CLIENT_SECRET')
OAUTH_PROVIDERS = [{
'name':'auth0',
'token_key': 'access_token',
'icon':'fa-at',
'url': AUTH0_URL,
'remote_app': {
'client_id': AUTH0_CLIENT_KEY,
'client_secret': AUTH0_CLIENT_SECRET,
'request_token_params': {
'scope': 'email openid profile'
},
'response_type': 'token_id',
'base_url': AUTH0_URL,
'access_token_url': os.path.join(AUTH0_URL, 'oauth/token'),
'authorize_url': os.path.join(AUTH0_URL, 'authorize'),
'access_token_method':'POST',
'request_token_url': os.path.join(AUTH0_URL, 'oauth/token'),
'api_base_url': AUTH0_URL,
}
}
]
CUSTOM_SECURITY_MANAGER = CustomSsoSecurityManager
I have already tried different values for the response_type (code, token, token_id).
Also tried to leave request_token_url empty and in that case the error changes because the user data appear to be an empty dictionary:
2021-10-13 15:52:10,358:WARNING:superset_config: user_data: {}
2021-10-13 15:52:10,358:WARNING:root:user_data: {}
2021-10-13 15:52:10,358:ERROR:flask_appbuilder.security.views:Error returning OAuth user info: 'email'
So I assume the token is actually returned and I cannot understand why Flask is complaining about the attribute "scope".
Tried this too, since it looked like very similar to my problem, but none of those configurations work for me.
Hope you have two files as custom_sso_security_manager.py and superset_config.py
Can you remove below two line from return and try(custom_sso_security_manager.py).
'first_name': me['given_name'],
'last_name': me['family_name'],
This is for future reference, although I accepted Kamal's answer.
It turned out that the right parameter to set the request token scopes was client_kwargs instead of request_token_params.
This is a working configuration to authenticate Superset against Auth0:
## Enable OAuth authentication
from flask_appbuilder.security.manager import (
AUTH_OAUTH,
)
from superset.security import SupersetSecurityManager
import json
import logging
import string
import random
nonce = ''.join(random.choices(string.ascii_uppercase + string.digits + string.ascii_lowercase, k = 30))
logger = logging.getLogger(__name__)
class CustomSsoSecurityManager(SupersetSecurityManager):
def oauth_user_info(self, provider, response=None):
if provider == 'auth0':
res = self.appbuilder.sm.oauth_remotes[provider].get('userinfo')
if res.raw.status != 200:
logger.error('Failed to obtain user info: %s', res.json())
return
me = res.json()
return {
'username' : me['email'],
'name' : me['name'],
'email' : me['email'],
}
AUTH_TYPE = AUTH_OAUTH
AUTH_USER_REGISTRATION = True
AUTH_USER_REGISTRATION_ROLE = "Public"
AUTH0_URL = os.getenv('AUTH0_URL')
AUTH0_CLIENT_KEY = os.getenv('AUTH0_CLIENT_KEY')
AUTH0_CLIENT_SECRET = os.getenv('AUTH0_CLIENT_SECRET')
OAUTH_PROVIDERS = [
{ 'name':'auth0',
'token_key':'access_token',
'icon':'fa-at',
'remote_app': {
'api_base_url': AUTH0_URL,
'client_id': AUTH0_CLIENT_KEY,
'client_secret': AUTH0_CLIENT_SECRET,
'server_metadata_url': os.path.join(AUTH0_URL, '.well-known/openid-configuration'),
'client_kwargs': {
'scope': 'openid profile email'
},
'response_type': 'code token',
'nonce': nonce,
}
}
]
CUSTOM_SECURITY_MANAGER = CustomSsoSecurityManager
As per Flask Documentation,
try to use client_kwargs instead of request_token_params key.
Sample:
{
'name':'google',
'icon':'fa-google',
'token_key':'access_token',
'remote_app': {
'client_id':'GOOGLE_KEY',
'client_secret':'GOOGLE_SECRET',
'api_base_url':'https://www.googleapis.com/oauth2/v2/',
'client_kwargs':{
'scope': 'email profile'
},
'request_token_url':None,
'access_token_url':'https://accounts.google.com/o/oauth2/token',
'authorize_url':'https://accounts.google.com/o/oauth2/auth'
}
},

Accessing a specific item in a JSON return in Rails

I have this JSON from the NHL API, and I tried to access the value gamesPlayed:
"teams"=>[
{
"id"=>5,
"name"=>"Pittsburgh Penguins",
"link"=>"/api/v1/teams/5",
"venue"=>{
"id"=>5034,
"name"=>"PPG Paints Arena",
"link"=>"/api/v1/venues/5034",
"city"=>"Pittsburgh",
"timeZone"=>{
"id"=>"America/New_York",
"offset"=>-5,
"tz"=>"EST"
}
},
"abbreviation"=>"PIT",
"teamName"=>"Penguins",
"locationName"=>"Pittsburgh",
"division"=>{
"id"=>18,
"name"=>"Metropolitan",
"nameShort"=>"Metro",
"link"=>"/api/v1/divisions/18",
"abbreviation"=>"M"
},
"conference"=>{
"id"=>6,
"name"=>"Eastern",
"link"=>"/api/v1/conferences/6"
},
"franchise"=>{
"franchiseId"=>17,
"teamName"=>"Penguins",
"link"=>"/api/v1/franchises/17"
},
"teamStats"=>[
{
"type"=>{
"displayName"=>"statsSingleSeason"
},
"splits"=>[
{
"stat"=>{
"gamesPlayed"=>16,
"wins"=>7,
"losses"=>6,
"ot"=>3,
"pts"=>17,
"ptPctg"=>"53.1",
"goalsPerGame"=>3.313,
"goalsAgainstPerGame"=>3.063,
"evGGARatio"=>1.0833,
"powerPlayPercentage"=>"23.4",
"powerPlayGoals"=>11.0,
"powerPlayGoalsAgainst"=>8.0,
"powerPlayOpportunities"=>47.0,
"penaltyKillPercentage"=>"84.0",
"shotsPerGame"=>32.625,
"shotsAllowed"=>33.6875,
"winScoreFirst"=>0.6,
"winOppScoreFirst"=>0.167,
"winLeadFirstPer"=>0.5,
"winLeadSecondPer"=>1.0,
"winOutshootOpp"=>0.333,
"winOutshotByOpp"=>0.444,
"faceOffsTaken"=>1035.0,
"faceOffsWon"=>534.0,
"faceOffsLost"=>501.0,
"faceOffWinPercentage"=>"51.6",
"shootingPctg"=>10.2,
"savePctg"=>0.909
},
"team"=>{
"id"=>5,
"name"=>"Pittsburgh Penguins",
"link"=>"/api/v1/teams/5"
}
},
{
"stat"=>{
"wins"=>"24th",
"losses"=>"15th",
"ot"=>"9th",
"pts"=>"24th",
"ptPctg"=>"19th",
"goalsPerGame"=>"8th",
"goalsAgainstPerGame"=>"19th",
"evGGARatio"=>"11th",
"powerPlayPercentage"=>"10th",
"powerPlayGoals"=>"22nd",
"powerPlayGoalsAgainst"=>"4th",
"powerPlayOpportunities"=>"31st",
"penaltyKillOpportunities"=>"1st",
"penaltyKillPercentage"=>"6th",
"shotsPerGame"=>"12th",
"shotsAllowed"=>"27th",
"winScoreFirst"=>"15th",
"winOppScoreFirst"=>"27th",
"winLeadFirstPer"=>"27th",
"winLeadSecondPer"=>"7th",
"winOutshootOpp"=>"25th",
"winOutshotByOpp"=>"25th",
"faceOffsTaken"=>"25th",
"faceOffsWon"=>"19th",
"faceOffsLost"=>"6th",
"faceOffWinPercentage"=>"8th",
"savePctRank"=>"13th",
"shootingPctRank"=>"12th"
},
"team"=>{
"id"=>5,
"name"=>"Pittsburgh Penguins",
"link"=>"/api/v1/teams/5"
}
}
]
}
],
"shortName"=>"Pittsburgh",
"officialSiteUrl"=>"http://pittsburghpenguins.com/",
"franchiseId"=>17,
"active"=>true
}
}
I am working in ruby on rails and would like to access the gamesPlayed value.
So far I have:
url = 'https://statsapi.web.nhl.com/api/v1/teams/5?expand=team.stats'
uri = URI(url)
response = Net::HTTP.get(uri)
response = JSON.parse(response)
#awayteamgamesplayed = response["teams"][0]["teamStats"]["stat"]["gamesPlayed"]
I can get to the team name using: response["teams"][away_team]["name"] but cant't work out gamesPlayed.
But it doesn't seem to work for gamesPlayed.
Value of teamStats is an Array. You need to access it via an index.
Same for splits
response["teams"][0]["teamStats"][0]["splits"][0]["stat"]["gamesPlayed"]
# => 16
teamStats is an array try this
response["teams"][0]["teamStats"][0]["stat"]["gamesPlayed"]

Node.js - Array is converted to object when sent in HTTP GET request query

The following Node.js code:
var request = require('request');
var getLibs = function() {
var options = { packages: ['example1', 'example2', 'example3'], os: 'linux', pack_type: 'npm' }
request({url:'http://localhost:3000/package', qs:options},
function (error , response, body) {
if (! error && response.statusCode == 200) {
console.log(body);
} else if (error) {
console.log(error);
} else{
console.log(response.statusCode);
}
});
}();
sends the following http GET request query that is received by like this:
{"packages"=>{"0"=>"example1", "1"=>"example2", "2"=>"example3"}, "os"=>"linux", "pack_type"=>"npm"}
How can I optimize this request to be received like this:
{"packages"=>["example1", "example2", "example3"], "os"=>"linux", "pack_type"=>"npm"}
Note. The REST API is built in Ruby on Rails
If the array need to be received as it is, you can set useQuerystring as true:
UPDATE: list key in the following code example has been changed to 'list[]', so that OP's ruby backend can successfully parse the array.
Here is example code:
const request = require('request');
let data = {
'name': 'John',
'list[]': ['XXX', 'YYY', 'ZZZ']
};
request({
url: 'https://requestb.in/1fg1v0i1',
qs: data,
useQuerystring: true
}, function(err, res, body) {
// ...
});
In this way, when the HTTP GET request is sent, the query parameters would be:
?name=John&list[]=XXX&list[]=YYY&list[]=ZZZ
and the list field would be parsed as ['XXX', 'YYY', 'ZZZ']
Without useQuerystring (default value as false), the query parameters would be:
?name=John&list[][0]=XXX&list[][1]=YYY&list[][2]=ZZZ
I finally found a fix. I used 'qs' to stringify 'options' with {arrayFormat : 'brackets'} and then concatinated to url ended with '?' as follows:
var request = require('request');
var qs1 = require('qs');
var getLibs = function() {
var options = qs1.stringify({
packages: ['example1', 'example2', 'example3'],
os: 'linux',
pack_type: 'npm'
},{
arrayFormat : 'brackets'
});
request({url:'http://localhost:3000/package?' + options},
function (error , response, body) {
if (! error && response.statusCode == 200) {
console.log(body);
} else if (error) {
console.log(error);
} else{
console.log(response.statusCode);
}
});
}();
Note: I tried to avoid concatenation to url, but all responses had code 400
This problem can be solved using Request library itself.
Request internally uses qs.stringify. You can pass q option to request which it will use to parse array params.
You don't need to append to url which leaves reader in question why that would have been done.
Reference: https://github.com/request/request#requestoptions-callback
const options = {
method: 'GET',
uri: 'http://localhost:3000/package',
qs: {
packages: ['example1', 'example2', 'example3'],
os: 'linux',
pack_type: 'npm'
},
qsStringifyOptions: {
arrayFormat: 'repeat' // You could use one of indices|brackets|repeat
},
json: true
};

Youtube api Where will write this code

Too bad my php knowledge.I'm using YouTube-api.Where will write this code: Retrieve Youtube Channel info for "Vanity" channel
If you are talking about this line :
GET https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC6ltI41W4P14NShIBHU8z1Q&key={YOUR_API_KEY}
You are simply making a get request, you can use file_get_contents to get the response for you :
$response = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC6ltI41W4P14NShIBHU8z1Q&key={YOUR_API_KEY}");
Two notes :
You have to replace {YOUR_API_KEY} with the developer key. You can easily request one from youtube: http://code.google.com/apis/youtube/dashboard/
This is just an example in one line of code, I suggest you use a better approach for making this request like the following :
// Encode the parameters of the link
function encode_param($params) {
foreach ($params as $field => $value){
$encoded_params[] = $field . '=' . urlencode($value);
}
return $encoded_params;
}
// Get the response
function get_response($url) {
$response = file_get_contents($url);
// If error, send message back to the client
if ($response === false) {
exit("Couldn't get response from the api");
}
return $response;
}
$params = array(
"part" => "snippet,contentDetails,statistics",
"id" => "UC6ltI41W4P14NShIBHU8",
"key" => "-----------", // Your API key
);
$encoded_params = encode_param($params);
$request_url = "https://www.googleapis.com/youtube/v3/channels?".implode('&', $encoded_params);
$response = get_response($request_url);
//............

Get LIKED youtube videos via API

I know how to get the count of 'liked' videos using the YouTube API, but I want to get a list of those videos.
After reading the docs, I think it can be done by getting the 'liked' playlist, but I do not know exactly how.
Can I get the 'liked' video list through the Javascript API?
If you're using v3 of the API, then you can get your liked video list. First, do a call to your channels feed, like this:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&key={YOUR_API_KEY}
Then, in the response, you'll have a list of related playlists -- one will be keyed "likes." Take that playlist ID and request its items feed:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={PLAYLIST_ID}&key={YOUR_API_KEY}
If you don't use v3 of the API, you probably won't have a lot of success in getting the liked videos.
As of 2020, the /videos endpoint lets you filter directly for liked videos, e.g.:
GET https://www.googleapis.com/youtube/v3/videos?myRating=like&part=snippet
Authorization: Bearer <oauth token>
If you pass the following arguments to playlistItems.list, you can get the liked videos' playlist associated with the authorized acccount.
auth: "your_auth_key"
playlistId: "LL"
Here's a code snippet from the script I ran to get the liked videos in a text file.
Note: I used the helper code provided in the YouTube API Documentation to get the authkey and pass it to my function.
// get all the liked videos by a channel
async function get_liked_playlist(authkey){
fs.writeFile("./output/"+"all_liked_videos"+".txt", "\n"+time_stamp, { flag: 'a+' }, e => console.log(e) );
let nextPageToken_ = null;
let text__ = "";
let i = 0;
do {
await API.playlistItems.list({
key: process.env.API_KEY,
auth: authkey,
part: "snippet",
maxResults: 50, // 50 is the max value
playlistId: "LL",
pageToken: nextPageToken_
})
.then(res => {
let results = res.data.items;
nextPageToken_ = res.data.nextPageToken;
results.forEach(item => {
// console.log(`Title: ${item.snippet.title}\tURL: https://youtu.be/${item.snippet.resourceId.videoId}`)
i++;
text__ += "\nTitle: "+item.snippet.title+"\tURL: https://youtu.be/"+item.snippet.resourceId.videoId;
});
console.log("items done: "+i+"\tnextPageToken: "+nextPageToken_);
})
.then( fs.writeFile("./output/"+"all_liked_videos"+".txt", text__ , { flag: 'a+' }, e => { if(e) console.log("error with fs\t"+e); }) )
.then( text__ = "" )
.catch( e => console.log("error here\t" + e) )
} while (nextPageToken_ != null)
if(text__.length>1) fs.writeFile("./output/"+"all_liked_videos"+".txt", text__ , { flag: 'a+' }, e => { if(e) console.log("error with fs\t"+e); });
}

Resources