Angular 4与Spring Boot如何导入Gmail/Yahoo/Hotmail联系人列表?有无免费API?
Hey there! Let's walk through how to implement contact import from Gmail, Yahoo, and Hotmail/Outlook using your Angular 4 + Spring Boot stack, plus cover the free APIs you can leverage.
To pull contacts from these platforms, you'll need to use OAuth 2.0 authorization to gain permission to access a user's contacts, then call each platform's dedicated contacts API. Your Angular 4 frontend will handle guiding users through the auth flow, while your Spring Boot backend manages the callback, retrieves access tokens, and fetches the actual contact data.
1. Gmail Contact Import (Google People API)
Google's People API is the modern replacement for the old Contacts API, and it has a generous free tier.
- Frontend (Angular 4) Steps:
- First, create a project in the Google Cloud Console, enable the People API, and set up an OAuth client ID (whitelist your Angular app's domain and your Spring Boot callback URL).
- Build the authorization URL and redirect the user to Google's login page:
// Example auth URL construction const googleAuthUrl = 'https://accounts.google.com/o/oauth2/v2/auth?' + 'client_id=YOUR_GOOGLE_CLIENT_ID&' + 'redirect_uri=YOUR_SPRING_BOOT_CALLBACK_ENDPOINT&' + 'scope=https://www.googleapis.com/auth/contacts.readonly&' + 'response_type=code&' + 'access_type=offline'; // Redirect user to auth page window.location.href = googleAuthUrl;
- Backend (Spring Boot) Steps:
- Capture the authorization
codeGoogle sends to your callback endpoint, then exchange it for an access token and refresh token. - Use the Google Java Client Library to call the People API and fetch contacts:
// Pseudocode (use the official Google Java SDK for production) GoogleCredential credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setClientSecrets(CLIENT_ID, CLIENT_SECRET) .build() .setFromTokenResponse(tokenResponse); // Token from code exchange PeopleService peopleService = new PeopleService.Builder(httpTransport, jsonFactory, credential) .setApplicationName("Your App Name") .build(); ListConnectionsResponse response = peopleService.people().connections() .list("people/me") .setPersonFields("names,emailAddresses") .execute(); List<Person> contacts = response.getConnections();
- Capture the authorization
- Free Tier: 10,000 free requests per month—plenty for small to medium projects.
2. Hotmail/Outlook Contact Import (Microsoft Graph API)
Microsoft Graph is the unified API for all Microsoft services, including Outlook contacts, and it offers solid free access.
- Frontend (Angular 4) Steps:
- Register an app in Azure Active Directory, add the
Contacts.Readpermission, and configure your Spring Boot callback URL as a redirect URI. - Build the Microsoft auth URL and redirect the user:
const outlookAuthUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?' + 'client_id=YOUR_AZURE_CLIENT_ID&' + 'redirect_uri=YOUR_SPRING_BOOT_CALLBACK_ENDPOINT&' + 'scope=https://graph.microsoft.com/contacts.read&' + 'response_type=code&' + 'prompt=consent'; window.location.href = outlookAuthUrl;
- Register an app in Azure Active Directory, add the
- Backend (Spring Boot) Steps:
- Exchange the authorization code for an access token, then use the Microsoft Graph Java SDK to fetch contacts:
// Pseudocode (use official Microsoft Graph Java SDK) IGraphServiceClient graphClient = GraphServiceClient.builder() .authenticationProvider(new AuthorizationCodeProvider( CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, AUTH_CODE, scopes )) .buildClient(); IContactCollectionPage contacts = graphClient.me().contacts() .buildRequest() .select("displayName,emailAddresses") .get();
- Exchange the authorization code for an access token, then use the Microsoft Graph Java SDK to fetch contacts:
- Free Tier: Free access for personal accounts, with limits that work well for small projects.
3. Yahoo Contact Import (Yahoo Contacts API)
Yahoo's Contacts API requires a developer account, but it's free for non-commercial use.
- Frontend (Angular 4) Steps:
- Create an app in the Yahoo Developer Portal, whitelist your callback URL, and get your client ID/secret.
- Redirect the user to Yahoo's auth page:
const yahooAuthUrl = 'https://api.login.yahoo.com/oauth2/request_auth?' + 'client_id=YOUR_YAHOO_CLIENT_ID&' + 'redirect_uri=YOUR_SPRING_BOOT_CALLBACK_ENDPOINT&' + 'scope=sdct-w&' + // Permission to read contacts 'response_type=code'; window.location.href = yahooAuthUrl;
- Backend (Spring Boot) Steps:
- Exchange the code for an access token, then call Yahoo's Contacts API endpoint (
/v1/user/{user-id}/contacts) to retrieve contact data. Be sure to parse Yahoo's specific JSON response structure.
- Exchange the code for an access token, then call Yahoo's Contacts API endpoint (
- Free Tier: Free for non-commercial applications; check Yahoo's developer docs for exact limits.
Key Integration Tips for Angular 4 + Spring Boot
- Keep sensitive data server-side: Never handle access tokens in your Angular app—all token exchanges and API calls should happen in Spring Boot.
- CORS configuration: Make sure your Spring Boot backend allows requests from your Angular app's domain to avoid cross-origin errors.
- Error handling: Account for auth failures, expired tokens (use refresh tokens to get new access tokens), and rate limits from the APIs.
- Offline access: Use the
access_type=offlineparameter (for Google) or equivalent for other platforms to get refresh tokens, so you can fetch contacts later without re-authenticating the user.
Are There Free APIs Available?
Absolutely! All the APIs mentioned above are free to use for most use cases:
- Google People API
- Microsoft Graph API
- Yahoo Contacts API
You just need to register a developer account with each platform, create an app, and follow their OAuth 2.0 flow. None of these require paid plans for small-scale use.
内容的提问来源于stack exchange,提问作者Priyatham Krishna

