list blogs list of blogger in Google PHP SDK - google-api

I am using Google API SDK in CakePHP application.
I have to list the blogs owned by the user after authenticating the application.
The user will select from the list of blogs and I will record the blog ID to make further requests.
According to the documentation, we need to make GET request to
GET https://www.googleapis.com/blogger/v3/users/self/blogs?key=YOUR-API-KEY
But I have my API Key and user's access token.
How can I list the blogs using the access token?
I tried using the SDK Services
$client = new \Google_Client();
$client->setApplicationName(GOOGLE_APP_NAME);
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setAccessToken($access_token);
$client->setAccessType("offline"); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope('https://www.googleapis.com/auth/userinfo.email');
$client->addScope('https://www.googleapis.com/auth/blogger');
$callback_url = Router::url(['prefix' => 'dashboard', 'controller' => 'SharingLogin', 'action' => 'bloggerCallback'], true);
$client->setRedirectUri($callback_url);
$service = new \Google_Service_Blogger($client);
$listblog = $service->blogs;
debug($listblog);
But this lists nothing and empty array object.

Related

External API auth should be a ServiceProvider or Controller in Laravel?

Im building an app using Laravel, that app will allow the customers to send and get contacts from/to Mautic (email marketing software) via API, but first the app need to get an autorization to use Mautic API and store the user credintals to database for future use.
This is an example for how i make an autorization to connect to that api
$settings = [
'userName' => '', // Create a new user
'password' => '', // Make it a secure password
];
// Initiate the auth object specifying to use BasicAuth
$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings, 'BasicAuth');
and then i can get the contact by id using this
$api = new MauticApi(); //This class is from the API package
$contactApi = $api->newApi('contacts', $auth, $apiUrl);
$response = $contactApi->get($id);
So my question is how can i organize that logic, should i just put all of it in a controller or it's better to create a service provider which should be responsible of the autorization and then return $auth handle that i can use then later for each customer, and if it's better to user the serviceprovider approch then i'm wondering how can i do that, should i just put the autorization logic in the boot method?

Where does ASP.NET core API with social logins enabled store access token?

I am using ASP.NET Core Identity with social login providers in my ASP.NET Core 3.1 API. I can login with the configured providers successfully. After logging in, I have a controller that needs access to the access_token that was provided by the social authentication provider to query for additional data (Facebook in this case).
Does ASP.NET Core Identity store the access token from the social login provider anywhere by default or is that my responsibility to write it to a cookie or session in the ExternalLogin callback page of the scaffolded identity code after calling GetExternalLoginInfoAsync()?
It seems GetExternalLoginInfoAsync() only returns information in the ExternalLogin callback as calling this same method from my controller always returns NULL. So maybe the cookie is removed after successfully logging in. This seems a bit strange as I would expect it to be able to return the related record stored stored in the AspNetUserLogin table as the user is signed in when calling that method from the controller.
I've also tried using the following code to store all the tokens, but can't seem to find them anywhere. Where does it store them?
services.AddAuthentication()
.AddFacebook(fb =>
{
fb.AppId = Configuration["FacebookAppId"];
fb.AppSecret = Configuration["FacebookAppSecret"];
fb.SaveTokens = true;
fb.Scope.Add("user_birthday");
fb.Scope.Add("user_friends");
fb.Events.OnCreatingTicket = ctx =>
{
List<AuthenticationToken> tokens = ctx.Properties.GetTokens().ToList();
tokens.Add(new AuthenticationToken()
{
Name = "TicketCreated",
Value = DateTime.UtcNow.ToString()
});
ctx.Properties.StoreTokens(tokens);
return Task.CompletedTask;
};
})

Laravel socialite

I have used laravel auth and socialite package in my web app. I have followed https://www.youtube.com/watch?v=uavoKwhGBKI&t=932s link and it working fine.To be brief, If I register using socialite it fetches the name of the user and email but needs to be filled other details as DOB and password but if I submit without filling that it shows 500 error while if I register without socialite then my validation works fine.The registration page is same.
Socialite only gives you access to a select set of data returned from a successful connection.
# Retrieving User Details
https://laravel.com/docs/6.x/socialite#retrieving-user-details
$user = Socialite::driver('github')->user();
// OAuth Two Providers
$token = $user->token;
$refreshToken = $user->refreshToken; // not always provided
$expiresIn = $user->expiresIn;
// OAuth One Providers
$token = $user->token;
$tokenSecret = $user->tokenSecret;
// All Providers
$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();
# Modifying LoginController
https://scqq.blogspot.com/2017/11/laravel-55-socialite-login-with-twitter.html
This guide you followed does not actually register a user by social platform. It only pre-populates the default Laravel registration form with the fields name and email as shown below. This is where you can add another property from above, such as the user's avatar, if desired. You would also need to add the corresponding field to the registration form.
return view('auth.register', [
'name' => $userSocial->getName(),
'email' => $userSocial->getEmail(),
// ... 'avatar' => $userSocial->getAvatar(),
]);
Google is NEVER going to give you someone's password!
The whole point of Socialite is to allow Google (or the selected provider) to authenticate the user — not your application.
If you wish to actually register a user with Socialite (without any additional forms or setting a password), you will need to modify or extend RegisterController.php to be able to support this.

How to post image on facebook timeline using graph api 4.0?

I am creating an API to post on my Facebook timeline using graph API 4.0. For this, i am first creating an album and then post image in that album.
here is the code
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
//Create an album
$album_details = array(
'message'=> 'Album desc',
'name'=> 'Album name'
);
$create_album = $facebook->apiRequest('/me/albums/{user-access-token}', 'post', $album_details);
dd($create_album);
}
which returns an error
Raw Provider API response: {\"error\":{\"message\":\" This object does not exist or does not support this action\"
But when i gave page token instead of a user access token , it works fine. I am new to this. So, i have no idea how to solve this.
The Facebook API no longer permits publishing to personal profile feeds.
https://developers.facebook.com/docs/graph-api/reference/v4.0/user/feed#publish
As of April 24,2018, the publish_actions permission has been removed. Please see the Breaking Changes Changelog for more details. To provide a way for your app users to share content to Facebook, we encourage you to use our Sharing products instead.

Token Passport Users

I'm new to building APIs, and I'm using Laravel and some packages, like Passport.
In this project I'm creating an API to communicate with a mobile app and make some tasks, like creating users and adding some information related to the users.
I'm already getting the idea of how it works with tokens, and I already have almost everything done. My only question is, for example, before I create a register user to receive the token I have other information being presented in the mobile app, like news, and some listing information that is not needed to login or register.
In my API I already have these routes ready, but I cant access this information because I need a token.
How do iI handle this situation? When I need to access information, where I cant present it?
Use your controllers to retrieve the data you need from your database and then return a response with the data and create a token for the user:
...
$status = 200;
$response = [
'data' => Your data (user info, listings, news...),
'token' => Auth::user()->createToken("YourTokenName")->accessToken,
];
return response()->json($response, $status);
Then you could store that token in localStorage:
axios.post('your-api-route')
.then((response) => {
localStorage.setItem(`${"YourTokenName"}.jwt`, response.token);
...
})
And finally attach that token to the api routes that require authentication:
axios.defaults.headers.common['Authorization'] = 'Bearer ' + localStorage.getItem(`${YourTokenName}.jwt`);

Resources