{"id":13742914,"url":"https://github.com/rubenCodeforges/ng-gapi","last_synced_at":"2025-05-09T00:32:01.448Z","repository":{"id":20334013,"uuid":"89567170","full_name":"rubenCodeforges/ng-gapi","owner":"rubenCodeforges","description":"ng-gapi a Google api module for Angular 6+","archived":false,"fork":false,"pushed_at":"2024-05-03T09:01:49.000Z","size":3123,"stargazers_count":127,"open_issues_count":23,"forks_count":42,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-29T17:14:03.285Z","etag":null,"topics":["angular2","angular4","api","gapi","google","google-api","ng-gapi"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rubenCodeforges.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-27T07:12:28.000Z","updated_at":"2024-04-22T22:18:52.000Z","dependencies_parsed_at":"2024-08-24T00:02:02.068Z","dependency_job_id":null,"html_url":"https://github.com/rubenCodeforges/ng-gapi","commit_stats":{"total_commits":82,"total_committers":10,"mean_commits":8.2,"dds":"0.18292682926829273","last_synced_commit":"69e6fec625bc7b124fc9a1b3983eada04b8d1226"},"previous_names":["rubencodeforges/angular2-google-api"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenCodeforges%2Fng-gapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenCodeforges%2Fng-gapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenCodeforges%2Fng-gapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenCodeforges%2Fng-gapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rubenCodeforges","download_url":"https://codeload.github.com/rubenCodeforges/ng-gapi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253171056,"owners_count":21865275,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular2","angular4","api","gapi","google","google-api","ng-gapi"],"created_at":"2024-08-03T05:00:37.742Z","updated_at":"2025-05-09T00:32:00.880Z","avatar_url":"https://github.com/rubenCodeforges.png","language":"TypeScript","readme":"### Angular 9+ Google api module (ng-gapi)\n\nThis module will add the google api to your project.\nIt wraps the Gapi in to a service layer allowing to work with Gapi\nin a Angular 9+ project.\n\nmade by [codeforges](https://codeforges.com)\n\n##### Latest News \n\nWe have started to work on our [video tutorials series](https://www.youtube.com/watch?v=nz6yFTyLbAQ\u0026list=PLq1kZ5GbKd4qyDcK3IHGSi4FDAL6fRZeL), Angular + NestJs a full fledged application.\n\n\n#### Latest change\n\n- Requires now Typescript version 3.8.3 or higher\n- Requires Angular9 or higher\n\n#### Installation\n\n```js\nnpm install ng-gapi\n```\n\n#### Usage\n\n[DEMO stackblitz](https://stackblitz.com/edit/ng-gapi-example)\n\nTo use the `ng-gapi` simply add `GoogleApiModule` to your module imports\nand set the configuration.\n\n##### ClientConfig interface\nBellow are all available parameters that can be provided in the `forRoot()` method.\n```typescript\nexport interface NgGapiClientConfig extends ClientConfig {\n    discoveryDocs: string[];\n}\n\n\n//And the extended ClientConfig\ninterface ClientConfig {\n    /**\n     * The app's client ID, found and created in the Google Developers Console.\n     */\n    client_id?: string;\n\n    /**\n     * The domains for which to create sign-in cookies. Either a URI, single_host_origin, or none.\n     * Defaults to single_host_origin if unspecified.\n     */\n    cookie_policy?: string;\n\n    /**\n     * The scopes to request, as a space-delimited string. Optional if fetch_basic_profile is not set to false.\n     */\n    scope?: string;\n\n    /**\n     * Fetch users' basic profile information when they sign in. Adds 'profile' and 'email' to the requested scopes. True if unspecified.\n     */\n    fetch_basic_profile?: boolean;\n\n    /**\n     * The Google Apps domain to which users must belong to sign in. This is susceptible to modification by clients,\n     * so be sure to verify the hosted domain property of the returned user. Use GoogleUser.getHostedDomain() on the client,\n     * and the hd claim in the ID Token on the server to verify the domain is what you expected.\n     */\n    hosted_domain?: string;\n\n    /**\n     * Used only for OpenID 2.0 client migration. Set to the value of the realm that you are currently using for OpenID 2.0,\n     * as described in \u003ca href=\"https://developers.google.com/accounts/docs/OpenID#openid-connect\"\u003eOpenID 2.0 (Migration)\u003c/a\u003e.\n     */\n    openid_realm?: string;\n\n    /**\n     * The UX mode to use for the sign-in flow.\n     * By default, it will open the consent flow in a popup.\n     */\n    ux_mode?: \"popup\" | \"redirect\";\n\n    /**\n     * If using ux_mode='redirect', this parameter allows you to override the default redirect_uri that will be used at the end of the consent flow.\n     * The default redirect_uri is the current URL stripped of query parameters and hash fragment.\n     */\n    redirect_uri?: string;\n  }\n```\n\n##### Example:\n```typescript\nimport {\n    GoogleApiModule, \n    GoogleApiService, \n    GoogleAuthService, \n    NgGapiClientConfig, \n    NG_GAPI_CONFIG,\n    GoogleApiConfig\n} from \"ng-gapi\";\n\nlet gapiClientConfig: NgGapiClientConfig = {\n    client_id: \"CLIENT_ID\",\n    discoveryDocs: [\"https://analyticsreporting.googleapis.com/$discovery/rest?version=v4\"],\n    scope: [\n        \"https://www.googleapis.com/auth/analytics.readonly\",\n        \"https://www.googleapis.com/auth/analytics\"\n    ].join(\" \")\n};\n\n@NgModule({\n    imports: [\n        //...\n          GoogleApiModule.forRoot({\n            provide: NG_GAPI_CONFIG,\n            useValue: gapiClientConfig\n          }),\n        //...\n    ]\n})\nexport MyModule {}\n```\n\n\n\nNow you will have Access to the GoogleApi service.\nThe service has a a event method `onLoad(callback)`\nThis event will fire when the gapi script is loaded.\n\nUsage example :\n\n```typescript\nexport class FooService {\n    constructor(gapiService: GoogleApiService) {\n        gapiService.onLoad().subscribe(()=\u003e {\n           // Here we can use gapi\n           \n        });\n    }\n}\n```\n\nAlso check the example folder with a google api reports module\n\n#### GoogleAuthService \nThe module has a GoogleAuth service which allows you to work with \nthe google auth\n\nUsage:\n```typescript\n//Example of a UserService \n\n@Injectable()\nexport class UserService {\n    public static SESSION_STORAGE_KEY: string = 'accessToken';\n    private user: GoogleUser;\n    \n    constructor(private googleAuth: GoogleAuthService){ \n    }\n    \n    public getToken(): string {\n        let token: string = sessionStorage.getItem(UserService.SESSION_STORAGE_KEY);\n        if (!token) {\n            throw new Error(\"no token set , authentication required\");\n        }\n        return sessionStorage.getItem(UserService.SESSION_STORAGE_KEY);\n    }\n    \n    public signIn(): void {\n        this.googleAuth.getAuth()\n            .subscribe((auth) =\u003e {\n                auth.signIn().then(res =\u003e this.signInSuccessHandler(res));\n            });\n    }\n    \n    private signInSuccessHandler(res: GoogleUser) {\n            this.user = res;\n            sessionStorage.setItem(\n                UserService.SESSION_STORAGE_KEY, res.getAuthResponse().access_token\n            );\n        }\n}\n```\n\nLets go step by step through the example\n\n1. We create a angular Injectable() \"service\"\n2. The static property `SESSION_STORAGE_KEY` is just a sugar to store string in a property rather then hardcode\n3. in the constructor we inject the GoogleAuthService and making it a private property of our User class\n4. no we have 2 public methods , sign in and get token. The signIn should be used at user login page , it will open the google \nauth popup. \n5. The get token method is used for http request to google resource where a authentication is required.\n\n#### Batch requests\n\nFrom gapi docs https://developers.google.com/api-client-library/javascript/features/batch\nwe should use `gapi.client.newBatch()`\n\nBut in our case we have typings and OOP, so we can do this:\n\n```typescript\nexport class FooService {\n    constructor(gapiService: GoogleApiService) {\n        gapiService.onLoad().subscribe(()=\u003e {\n           const myBatch: HttpBatch = new HttpBatch();\n           myBatch.add(\n               // your request\n           );\n        });\n    }\n}\n\n```\n\n\n#### Configurations\nThe GoogleApiConfig class provides the required configuration for the Api\n\nConfiguration is easy to use. The GoogleApiModule has a static method which sets the configs. \nAs shown in the example you simply provide a configuration object of type `ClientConfig`.\n```typescript\n {\n   client_id: \"your client id\",\n   discoveryDocs: [\"url to discovery docs\", \"another url\"],\n   scope: \"space separated scopes\"\n}\n```\nConfigure them according your google app configurations and resource scope.\n\n- To get the clientId see in your [developer console](https://console.developers.google.com/apis/credentials)\n- The discoveryDoc is in the resource description, here an example for\n [Reporting API v4](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/)\n- The scope is also in the documentation of the specific API , example for [Reporting API v4](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#authorization)\n\n\n#### Promotion\nWe are providing Web Development and Consulting Services.\n[codeforges.com](https://codeforges.com)\n","funding_links":[],"categories":["Resources and Inspiration"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FrubenCodeforges%2Fng-gapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FrubenCodeforges%2Fng-gapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FrubenCodeforges%2Fng-gapi/lists"}