YYour NameAccount Kit
c647687bAdd on Jun 10History
/*
 * Copyright (c) 2026 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.huawei.accountkit.userinfo;

import com.huawei.accountkit.userinfo.req.GetMobileByAccessTokenReq;
import com.huawei.accountkit.userinfo.resp.GetMobileByAccessTokenResp;
import com.huawei.accountkit.util.CallUtils;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取华为账号用户信息-获取手机号
 *
 * @author accountkit
 * @since 2026-04-14
 */
public class GetMobileByAccessTokenService {
    private static final String URL = "https://account.cloud.huawei.com/rest.php?nsp_svc=GOpen.User.getInfo";

    /**
     * 获取华为账号用户信息-获取手机号
     * 
     * @param req GetMobileByAccessTokenReq
     * @return GetMobileByAccessTokenResp
     * @throws IOException IOException
     */
    public static GetMobileByAccessTokenResp call(GetMobileByAccessTokenReq req) throws IOException {
        HttpPost httpPost = new HttpPost(URL);
        List<NameValuePair> request = new ArrayList<>();
        request.add(new BasicNameValuePair("access_token", req.getAccessToken()));
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.setEntity(new UrlEncodedFormEntity(request));

        // 使用默认异常处理逻辑,自定义异常处理请使用api CallUtils#remoteCall(HttpUriRequest, BiFunction<CloseableHttpResponse,String,E>)
        String res = CallUtils.remoteCall(httpPost);

        return CallUtils.parseObject(res, GetMobileByAccessTokenResp.class);
    }
}