Reputation: 11
this is my first time using Google API, and i got difficulty about google-people api, can anybody here explain what needed header/body data for auth (i use https://crystal-lang.org/api/latest/OAuth2.html), and please share a simple code (vanilla / without library) in your favorite programming language ^^
Upvotes: 1
Views: 520
Reputation: 7326
Follow steps described in Get Ready to Use the People API. There you will find examples written in Java, Python, PHP, .NET.
Let's assume you finished with step 1 and 2. Here is a Crystal code to make an authorization request:
require "oauth2"
client_id = "CLIENT_ID"
client_secret = "CLIENT_SECRET"
scope = "profile"
redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
client = OAuth2::Client.new(
"accounts.google.com",
client_id,
client_secret,
authorize_uri: "/o/oauth2/v2/auth",
redirect_uri: redirect_uri
)
authorize_uri = client.get_authorize_uri(scope)
authorize_uri #=> https://accounts.google.com/o/oauth2/v2/auth?client_id=CLIENT_ID&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcontacts.readonly
Open authorize link in your browser, allow access to your data and you will get a token required for the next step.
authorization_code = code # authorization code taken from the previous step
client = OAuth2::Client.new(
"www.googleapis.com",
client_id,
client_secret,
token_uri: "/oauth2/v4/token",
redirect_uri: redirect_uri
)
access_token = client.get_access_token_using_authorization_code(authorization_code)
client = HTTP::Client.new("people.googleapis.com", tls: true)
access_token.authenticate(client)
response = client.get "/v1/people/me?personFields=names"
response.body # a json that contains my name
Upvotes: 4