在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):google/googleapis.dart开源软件地址(OpenSource Url):https://github.com/google/googleapis.dart开源编程语言(OpenSource Language):Dart 100.0%开源软件介绍(OpenSource Introduction):
Google API Client Libraries with DartGoogle provides a large set of APIs that your applications can use to interact with Google products such as Google Drive, Gmail, Cloud Datastore, and Cloud Storage. These APIs are accessible via REST-like interfaces. The Dart team provides the googleapis and googleapis_beta packages, which provide generated Dart libraries for these APIs. Using the APIs, which are available for both web apps and cloud apps, is more convenient and less error-prone than using the raw REST protocol. To use any of these APIs, you need a Cloud Project. In addition, based on the API being used, you need either an API key or a Client ID. And finally, depending on what kind of application is being used, you might need a Service Account. All these items are available through Google Developers Console when you create a project. Below are a few scenarios:
To set up authentication and authorization for your app, you need to create a project, create the authentication credentials, and activate the API you want to use, all of which you do at Google Developers Console. Support for Google APIs is in two packages—googleapis and googleapis_beta. The googleapis package contains the APIs for all the APIs that are available in a stable version. The googleapis_beta package contains the APIs that are currently in beta and possibly only available through a Limited Preview program. A third package, googleapis_auth, provides a way to obtain OAuth 2.0 credentials and an authenticated HTTP client required to use these APIs. The rest of this document describes the following:
Some of the APIs you can useHere are just a few of the APIs your Dart programs can use.
You can see the full list of APIs at googleapis and googleapis_beta. OAuth 2.0Applications sometimes require OAuth 2.0 for authentication and authorization. Whether an application needs OAuth 2.0 depends on the API being used and the kind of data being accessed. For example, an application needs OAuth 2.0 if it needs user consent. An application sometimes needs OAuth 2.0 depending on the following properties:
The OAuth 2.0 protocol provides different flows for different types of applications, including web server, installed, and client-side applications. For example, the following diagram illustrates the flow of communication for client-side applications: Using OAuth 2.0 to Access Google APIs gives a detailed overview of the OAuth 2.0 authorization scenarios that Google supports.What do you need?To write a program that interacts with a Google product, you need several tools and packages.
A web application exampleLet's look at a web application example that uses the googleapis Google Drive API to retrieve documents from a user's Google Drive. The following screenshot shows the app after it has retrieved a list of spreadsheets from a user's Google Drive and displayed them on the page. You can find the source code for this example, drive_search_web, and others, at googleapis_examples on GitHub. The section Example explained describes the code in the drive_search_web example related to googleapis and googleapis_auth. But first let's look at how to run the example. Running the exampleTo run this example, you need to create a project, activate an API, and create a Client ID in Google Developers Console. This section provides instructions about how to do these for this particular example. For complete information and detailed explanations, refer to Google Developers Console Help. Create project
Activate the Drive API
Create a client ID
Run the example
This example, drive_search_web, uses the Google Drive API and the OAuth 2.0 API to access the user's files on Google Drive. Let's look at the relevant parts of the example.
dependencies:
googleapis_auth: '>=0.1.0 < 0.2.0'
googleapis: '>=0.1.1 < 0.2.0'
import 'package:googleapis_auth/auth_browser.dart' as auth;
import 'package:googleapis/drive/v2.dart' as drive;
final identifier = new auth.ClientId(
"<custom-app-id>.apps.googleusercontent.com",
null);
final scopes = [drive.DriveApi.DriveScope];
authorizedClient(loginButton, identifier, scopes).then((client) {
...
} Let's take a look at authorizedClient(). Future authorizedClient(ButtonElement loginButton, auth.ClientId id, scopes) {
return auth.createImplicitBrowserFlow(id, scopes)
.then((auth.BrowserOAuth2Flow flow) {
return flow.clientViaUserConsent(forceUserConsent: false).catchError((_) {
loginButton.text = '';
return loginButton.onClick.first.then((_) {
return flow.clientViaUserConsent(forceUserConsent: true);
});
}, test: (error) => error is auth.UserConsentException);
});
} First the code initializes the browser flow as illustrated in the
OAuth 2.0 diagram above.
Then the program attempts to proceed without user consent
using flow.clientViaUserConsent() with
var api = new drive.DriveApi(client);
Future<List<drive.File>> searchTextDocuments(drive.DriveApi api,
int max,
String query) {
var docs = [];
Future next(String token) {
// The API call returns only a subset of the results. It is possible
// to query through the whole result set via "paging".
return api.files.list(q: query, pageToken: token, maxResults: max)
.then((results) {
docs.addAll(results.items);
// If we would like to have more documents, we iterate.
if (docs.length < max && results.nextPageToken != null) {
return next(results.nextPageToken);
}
return docs;
});
}
return next(null);
} Using a service account for a Cloud APIFor APIs such as Cloud Datastore and Cloud Storage you need a Google service account, which is an account that represents your application instead of an individual end user. You can create one when creating a new Client ID: just click Service account instead of Web application. The example cloudstorage_upload_download_service_account needs a service account because it uses the Google Cloud Storage API. Let's take a quick look at the relevant code. The following code creates account credentials from the service account specified in the details of the given JSON string. After you create the service account, you can download the JSON from the Cloud Console. Then enter the appropriate information to replace the placeholders. final accountCredentials = new auth.ServiceAccountCredentials.fromJson(r'''
{
"private_key_id": "<please fill in>",
"private_key": "<please fill in>",
"client_email": "<please fill in>@developer.gserviceaccount.com",
"client_id": "<please fill in>.apps.googleusercontent.com",
"type": "service_account"
}
'''); The program selects the scopes it will use and gets the StorageAPI, just like the previous example retrieved the Drive API. final scopes = [storage.StorageApi.DevstorageFullControlScope];
...
var api = new storage.StorageApi(client); Using the credentials and scopes parameters, a call to clientViaServiceAccount gets an authenticated client that can use the Storage APIs. auth.clientViaServiceAccount(accountCredentials, scopes).then((client) {
...
var regexp = new RegExp(r'^gs://([^/]+)/(.+)$');
switch (args.first) {
case 'upload':
...
} Note that the code shown above creates accountCredentials, which identifies this application. The code specified by then() processes the command-line arguments and uploads or downloads a file as indicated. The following line of code uses the Storage API to upload a media file to Google Cloud Storage. return api.objects.insert(null, bucket, name: object, uploadMedia: media); Resource linksHere's a convenient list of the resources you might need:
ContributeThe following notes for those who are contributing to this package. If you are only using this package, you can skip this section. If you are only making changes to $ pushd googleapis_auth
$ pub get
$ pub run test
$ dart format . --fix
$ popd Otherwise...
Note: You may need to reset some of the downloaded discovery documents, such as drive, or prediction.
What next?
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论