dongmei8209 2016-11-29 22:27
浏览 24

api请求错误的地方

I'm using google places api to retrieve all results related with a search, so this is the things that I'm doing

type Places struct {
    ApiKey string
    Cx string
    c *maps.Client
}

func NewPlacesSearch(apiKey, cx string ) (*Places,error) {
    c, err := maps.NewClient(maps.WithAPIKey(apiKey))
    if err != nil {
        return nil, err
    }
    p := &Places{ApiKey:apiKey, Cx: cx, c:c}
    return p,nil
}

// Search returns all the results respect a keywords
func (p *Places) Search(search string) ([]*maps.PlacesSearchResponse, error) {
    results := make([]*maps.PlacesSearchResponse,0)
    search = url.QueryEscape(search)
    response, err := p.makeSearch(search, "")
    if err != nil {
        return results, err
    }
    results = append(results,response)
    fmt.Printf("%+v
",response)
    done:= false
    if response.NextPageToken == "" {
        done = true
    }
    for !done {
        response, err = p.makeSearch(search, response.NextPageToken)
        if err != nil {
            log.Fatalf("fatal error: %s", err)
        }
        fmt.Println(response)
        if response.NextPageToken == "" {
            done = true
        }
        results = append(results,response)
    }
    return results, nil
}

func (p *Places) makeSearch(search, token  string) (*maps.PlacesSearchResponse,error) {
    r := &maps.TextSearchRequest{Query: search ,PageToken:token}
    fmt.Printf("%+v
",r)
    response, err:= p.c.TextSearch(context.Background(),r)
    if err != nil {
        return  &maps.PlacesSearchResponse{}, err
    }
    return &response,nil
}

Im trying to iterate over all the pagination from google results, as you can see in first request inside of Search function

response, err := p.makeSearch(search, "")

The request is

{
    Query:my+text+to+search
    Location:<nil> 
    Radius:0 
    Language: 
    MinPrice: 
    MaxPrice: 
    OpenNow:false 
    Type: 
    PageToken:
 }

But when I try to do the second request (inside in the loop) I get a message

fatal error: maps: INVALID_REQUEST - 

The second request looks like:

{
    Query:my+text+to+search
    Location:<nil> 
    Radius:0 
    Language: 
    MinPrice: 
    MaxPrice: 
    OpenNow: false 
    Type: 
    PageToken: CuQB3AAAAMy72z91....
}

What am I doing wrong?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看